下边是一个合并单元格的例子,但运行后显示的标题是"A B C"...该怎么修改这几个值呢?搞了一天也没弄出来,请大家帮忙.谢谢了!import javax.swing.table.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
import javax.swing.*;import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;interface CMap
{
int span (int row, int column);
int visibleCell(int row, int column);
}class CTable extends JTable {
public CMap map;
public CTable(CMap cmp, TableModel tbl) {
super(tbl);
map=cmp;
setUI(new CTUI());
}
public Rectangle getCellRect(int row, int column, boolean includeSpacing){ if (map==null) return super.getCellRect(row,column, includeSpacing); int sk=map.visibleCell(row,column);
Rectangle r1=super.getCellRect(row,sk,includeSpacing); if (map.span(row,sk)!=0)
for (int i=1; i<map.span(row,sk); i++){
r1.width+=getColumnModel().getColumn(sk+i).getWidth();
}
return r1;
} Point s=new Point(1,1); public int columnAtPoint(Point p) {
int x=super.columnAtPoint(p);
if (x<0) return x;
int y=super.rowAtPoint(p);
return map.visibleCell(y,x);
}
}
class CTUI extends BasicTableUI
{
public void paint(Graphics g, JComponent c) {
Rectangle r=g.getClipBounds();
int firstRow=table.rowAtPoint(new Point(0,r.y));
int lastRow=table.rowAtPoint(new Point(0,r.y+r.height)); if (lastRow<0)
lastRow=table.getRowCount()-1;
for (int i=firstRow; i<=lastRow; i++)
paintRow(i,g);
}
private void paintRow(int row, Graphics g)
{
Rectangle r=g.getClipBounds();
for (int i=0; i<table.getColumnCount();i++)
{
Rectangle r1=table.getCellRect(row,i,true);
if (r1.intersects(r))
{
int sk=((CTable)table).map.visibleCell(row,i);
paintCell(row,sk,g,r1);
i+=((CTable)table).map.span(row,sk)-1;
}
}
}
private void paintCell(int row, int column, Graphics g,Rectangle area)
{
int verticalMargin = table.getRowMargin();
int horizontalMargin = table.getColumnModel().getColumnMargin(); Color c = g.getColor();
g.setColor(table.getGridColor());
g.drawRect(area.x,area.y,area.width,area.height);
g.setColor(c); area.setBounds(area.x + horizontalMargin/2, area.y + verticalMargin/2, area.width - horizontalMargin, area.height - verticalMargin); if (table.isEditing() && table.getEditingRow()==row && table.getEditingColumn()==column)
{
Component component = table.getEditorComponent();
component.setBounds(area);
component.validate();
}
else
{
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(renderer, row, column);
System.out.println(table.getRowCount());
if (component.getParent() == null)
{
rendererPane.add(component);
}
rendererPane.paintComponent(g, component, table, area.x, area.y,area.width, area.height, true);
}
}
}class CMaps implements CMap
{
public int span(int row, int column) {
if ((row==0) &&(column==0)) return 3;
if ((row==2)&& (column==0)) return 3;
if ((row==9)&&(column==5)) return 3;
return 1;
}
public int visibleCell(int row, int column) {
if ((row==0)&& (column>=3)&&(column<9))
return 3;
if ((row==4)&&(column>=7)&&(column <9))
return 7;
if ((row==9)&&(column>=5)&&(column<8))
return 5;
return column;
}
}public class test
{
public static void main(String args[])
{
JFrame jf=new JFrame("Table with cell spanning"); CMap m=new CMaps();
DefaultTableModel tm=new DefaultTableModel(30,3);
String[] title_name={"Data","Value",""};
JTable table=new JTable(tm); jf.getContentPane().add(new JScrollPane(new CTable(m,tm)));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.show();
}
}
import javax.swing.plaf.basic.*;
import java.awt.*;
import javax.swing.*;import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;interface CMap
{
int span (int row, int column);
int visibleCell(int row, int column);
}class CTable extends JTable {
public CMap map;
public CTable(CMap cmp, TableModel tbl) {
super(tbl);
map=cmp;
setUI(new CTUI());
}
public Rectangle getCellRect(int row, int column, boolean includeSpacing){ if (map==null) return super.getCellRect(row,column, includeSpacing); int sk=map.visibleCell(row,column);
Rectangle r1=super.getCellRect(row,sk,includeSpacing); if (map.span(row,sk)!=0)
for (int i=1; i<map.span(row,sk); i++){
r1.width+=getColumnModel().getColumn(sk+i).getWidth();
}
return r1;
} Point s=new Point(1,1); public int columnAtPoint(Point p) {
int x=super.columnAtPoint(p);
if (x<0) return x;
int y=super.rowAtPoint(p);
return map.visibleCell(y,x);
}
}
class CTUI extends BasicTableUI
{
public void paint(Graphics g, JComponent c) {
Rectangle r=g.getClipBounds();
int firstRow=table.rowAtPoint(new Point(0,r.y));
int lastRow=table.rowAtPoint(new Point(0,r.y+r.height)); if (lastRow<0)
lastRow=table.getRowCount()-1;
for (int i=firstRow; i<=lastRow; i++)
paintRow(i,g);
}
private void paintRow(int row, Graphics g)
{
Rectangle r=g.getClipBounds();
for (int i=0; i<table.getColumnCount();i++)
{
Rectangle r1=table.getCellRect(row,i,true);
if (r1.intersects(r))
{
int sk=((CTable)table).map.visibleCell(row,i);
paintCell(row,sk,g,r1);
i+=((CTable)table).map.span(row,sk)-1;
}
}
}
private void paintCell(int row, int column, Graphics g,Rectangle area)
{
int verticalMargin = table.getRowMargin();
int horizontalMargin = table.getColumnModel().getColumnMargin(); Color c = g.getColor();
g.setColor(table.getGridColor());
g.drawRect(area.x,area.y,area.width,area.height);
g.setColor(c); area.setBounds(area.x + horizontalMargin/2, area.y + verticalMargin/2, area.width - horizontalMargin, area.height - verticalMargin); if (table.isEditing() && table.getEditingRow()==row && table.getEditingColumn()==column)
{
Component component = table.getEditorComponent();
component.setBounds(area);
component.validate();
}
else
{
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(renderer, row, column);
System.out.println(table.getRowCount());
if (component.getParent() == null)
{
rendererPane.add(component);
}
rendererPane.paintComponent(g, component, table, area.x, area.y,area.width, area.height, true);
}
}
}class CMaps implements CMap
{
public int span(int row, int column) {
if ((row==0) &&(column==0)) return 3;
if ((row==2)&& (column==0)) return 3;
if ((row==9)&&(column==5)) return 3;
return 1;
}
public int visibleCell(int row, int column) {
if ((row==0)&& (column>=3)&&(column<9))
return 3;
if ((row==4)&&(column>=7)&&(column <9))
return 7;
if ((row==9)&&(column>=5)&&(column<8))
return 5;
return column;
}
}public class test
{
public static void main(String args[])
{
JFrame jf=new JFrame("Table with cell spanning"); CMap m=new CMaps();
DefaultTableModel tm=new DefaultTableModel(30,3);
String[] title_name={"Data","Value",""};
JTable table=new JTable(tm); jf.getContentPane().add(new JScrollPane(new CTable(m,tm)));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.show();
}
}
解决方案 »
- 谁知道JAVA数字签名啊?
- 为什么用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)而不用setDefaultCloseOperation(3)???
- 哪里有中文版 Eclipse 软见 可下的 ?
- 新手问一个连MYSQL的问题
- 关于实现接口的问题
- 请问java中能否实现在try中捕获到Exception后在catch中忽略掉。谢谢
- 一个简单的响应事件出错,谁帮调试一下,说说原因,谢谢!!
- 怎样创建类似JBuilder工具栏中的“|”,使用addSeparator() 好像不对
- 为何会报"无效的游标状态"?
- 初学者关于RMI的一个问题
- 转Java,求帮助!
- 请教一个困扰一下午的问题,组件重用?
{
public static void main(String args[])
{
JFrame jf=new JFrame("Table with cell spanning"); CMap m=new CMaps();
DefaultTableModel tm=new DefaultTableModel(30,3);
String[] title_name={"Data","Value",""};
tm.setColumnIdentifiers(title_name);
JTable table=new JTable(tm); jf.getContentPane().add(new JScrollPane(new CTable(m,tm)));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.show();
}
}