JTable表头分组 错位问题

      GroupableTableHeader header = (GroupableTableHeader)transmitterTable.getTableHeader();

ColumnGroup g_variables = new ColumnGroup("Output Variables");
g_variables.add(cm.getColumn(1));
g_variables.add(cm.getColumn(2));
g_variables.add(cm.getColumn(3));
g_variables.add(cm.getColumn(4));
g_variables.add(cm.getColumn(5));
//
class GroupableTableHeader extends JTableHeader {
private static final String uiClassID = "GroupableTableHeaderUI";
protected Vector columnGroups = null;

public GroupableTableHeader(TableColumnModel model) {
super(model);
setUI(new GroupableTableHeaderUI());
setReorderingAllowed(false);
}
public void updateUI(){
setUI(new GroupableTableHeaderUI());
}

public void setReorderingAllowed(boolean b) {
reorderingAllowed = false;
}

public void addColumnGroup(ColumnGroup g) {
if (columnGroups == null) {
columnGroups = new Vector();
}
columnGroups.addElement(g);
}

public Enumeration getColumnGroups(TableColumn col) {
if (columnGroups == null) return null;
Enumeration e = columnGroups.elements();
while (e.hasMoreElements()) {
ColumnGroup cGroup = (ColumnGroup)e.nextElement();
Vector v_ret = (Vector)cGroup.getColumnGroups(col,new Vector());
if (v_ret != null) {
return v_ret.elements();
}
}
return null;
}

public void setColumnMargin() {
if (columnGroups == null) return;
int columnMargin = getColumnModel().getColumnMargin();
Enumeration e = columnGroups.elements();
while (e.hasMoreElements()) {
ColumnGroup cGroup = (ColumnGroup)e.nextElement();
cGroup.setColumnMargin(columnMargin);
}
}

}


class GroupableTableHeaderUI extends BasicTableHeaderUI {

public void paint(Graphics g, JComponent c) {
Rectangle clipBounds = g.getClipBounds();
if (header.getColumnModel() == null) return;
//((GroupableTableHeader)header).setColumnMargin();注释掉就 OK
int column = 0;

Dimension size = header.getSize();
Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
Hashtable h = new Hashtable();
int columnMargin = header.getColumnModel().getColumnMargin();


Enumeration enumeration = header.getColumnModel().getColumns();

while (enumeration.hasMoreElements()) {

cellRect.height = size.height;
cellRect.y = 0;
TableColumn aColumn = (TableColumn)enumeration.nextElement();
Enumeration cGroups = ((GroupableTableHeader)header).getColumnGroups(aColumn);
if (cGroups != null) {
int groupHeight = 0;
while (cGroups.hasMoreElements()) {
ColumnGroup cGroup = (ColumnGroup)cGroups.nextElement();
Rectangle groupRect = (Rectangle)h.get(cGroup);
if (groupRect == null) {
groupRect = new Rectangle(cellRect);
Dimension d = cGroup.getSize(header.getTable());
groupRect.width = d.width;
groupRect.height = d.height;
h.put(cGroup, groupRect);
}
paintCell(g, groupRect, cGroup);
groupHeight += groupRect.height;
cellRect.height = size.height - groupHeight;
cellRect.y = groupHeight;
}
}

//cellRect.width = aColumn.getWidth() + columnMargin;
cellRect.width = aColumn.getWidth() ;

if (cellRect.intersects(clipBounds)) {
paintCell(g, cellRect, column);
}
cellRect.x += cellRect.width ;
column++;

}
}


class ColumnGroup {
protected TableCellRenderer renderer;
protected Vector v;
protected String text;
protected int margin=0;

public ColumnGroup(String text) {
this(null,text);
}

public ColumnGroup(TableCellRenderer renderer,String text) {
if (renderer == null) {
this.renderer = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JTableHeader header = table.getTableHeader();
if (header != null) {
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
setHorizontalAlignment(JLabel.CENTER);
setText((value == null) ? "" : value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
};
} else {
this.renderer = renderer;
}
this.text = text;
v = new Vector();
}


/**
* @param obj TableColumn or ColumnGroup
*/
public void add(Object obj) {
if (obj == null) { return; }
v.addElement(obj);
}


/**
* @param c TableColumn
* @param v ColumnGroups
*/
public Vector getColumnGroups(TableColumn c, Vector g) {
g.addElement(this);
if (v.contains(c)) return g;
Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object obj = e.nextElement();
if (obj instanceof ColumnGroup) {
Vector groups =
(Vector)((ColumnGroup)obj).getColumnGroups(c,(Vector)g.clone());
if (groups != null) return groups;
}
}
return null;
}

public TableCellRenderer getHeaderRenderer() {
return renderer;
}

public void setHeaderRenderer(TableCellRenderer renderer) {
if (renderer != null) {
this.renderer = renderer;
}
}

public Object getHeaderValue() {
return text;
}

public Dimension getSize(JTable table) {
Component comp = renderer.getTableCellRendererComponent(
table, getHeaderValue(), false, false,-1, -1);
int height = comp.getPreferredSize().height;
int width = 0;
Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object obj = e.nextElement();
if (obj instanceof TableColumn) {
TableColumn aColumn = (TableColumn)obj;
width += aColumn.getWidth();
width += margin;


} else {
width += ((ColumnGroup)obj).getSize(table).width;

}
}
return new Dimension(width, height);
}

public void setColumnMargin(int margin) {
this.margin = margin;
Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object obj = e.nextElement();
if (obj instanceof ColumnGroup) {
((ColumnGroup)obj).setColumnMargin(margin);
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值