选项而不是JTable

时间:2012-03-30 13:03:13

标签: java swing jtable renderer

  1. 我想为每一行表着色,        通过使用数据库的特定值

      spcification of mine database is some what like
    
       **
       id     name    color
       1      pavan   red
       2      xyz     white
       **
    
  2. 我可以使用

    为表格提供一种颜色
       table.setBackground(new color(158,145,134);       
    
    
    
    
       please provide me some solution or hint to approch towards answer,
       Thanks in advance.
    

1 个答案:

答案 0 :(得分:4)

这对JTable来说并不难!事实上,它非常容易!

请在此处查看我的回答:Highlight a cell in JTable via custom table model

为了方便而转载:


...子类JTable并覆盖JTable.preparedRenderer(TableCellRenderer renderer, int row, int column)。如果rowcolumn数字相同,您可以更改作为显示返回的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) {/***/}
    });