我想为每一行表着色, 通过使用数据库的特定值
spcification of mine database is some what like
**
id name color
1 pavan red
2 xyz white
**
我可以使用
为表格提供一种颜色 table.setBackground(new color(158,145,134);
please provide me some solution or hint to approch towards answer,
Thanks in advance.
答案 0 :(得分:4)
这对JTable来说并不难!事实上,它非常容易!
请在此处查看我的回答:Highlight a cell in JTable via custom table model
为了方便而转载:
...子类JTable并覆盖JTable.preparedRenderer(TableCellRenderer renderer, int row, int column)
。如果row
和column
数字相同,您可以更改作为显示返回的Component
的背景颜色(通常为JLabel
);
这是一个突出显示鼠标所在行的示例:
@Override
public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
final Component c = super.prepareRenderer(renderer, row, column);
if (row == this.itsRow) {
c.setBackground(Color.RED);
}
return c;
}
其中this.itsRow
是由MouseMotionListener
更新的int字段:
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
SubclassedJTable.this.itsRow = SubclassedJTable.this.rowAtPoint(e.getPoint());
SubclassedJTable.this.repaint();
}
public void mouseDragged(MouseEvent e) {/***/}
});