2013-03-02 130 views
-1

我在经常由不同线程更新的框架中有JTable。我正在寻找方法来改变JTable,不论其内容的单元格的背景颜色,像无条件地更改JTable单元格的背景颜色

JTable.setColorAt(Color.YELLOW, 0, 0) 

有像什么这个存在吗?如果没有,是否有像这样为像我这样的初学者写这样的东西的方法?

请帮忙.............

+0

@Audrius:它是相似的,但不是重复的。该链接需要更改编辑的单元格的背景,而不是所有单元格。 – 2013-03-02 17:34:04

+0

没有问题看@Ravindra Gullapalli的回答,有构造函数Object value,boolean isSelected,boolean hasFocus,int row,int column,每个参数都是关于你的问题尤其是int row,int column,那么只测试'conditions == int row' &&'conditions == int column' then'setBackground(Color.YELLOW)' – mKorbel 2013-03-02 18:47:05

回答

4

定义自己的单元格渲染器类设置背景颜色像这样

public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer { 
    public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) { 
     java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
     cellComponent.setBackground(java.awt.Color.YELLOW); 
     return cellComponent; 
    } 
} 

细胞,并将其连接到您的表

MyCellRenderer mcr = new MyCellRenderer(); 
for (int columnIndex = 0; columnIndex < myTable.getColumnCount(); columnIndex ++) { 
      myTable.getColumnModel().getColumn(columnIndex).setCellRenderer(mcr); 
     } 
+0

感谢您的帮助 对不起,从我身边拖延了很久,我陷入了两周的困境......。 。我通过添加条件尝试了这一点,但单元格颜色只在选中时才会更改。任何建议? – gnmanoj 2013-03-20 14:46:16

+0

您的问题被标记为重复,并指定了其他问题的链接。也看看这个。在这里讨论'prepareEditor'。 – 2013-03-20 14:52:56

相关问题