我有4列JTable。我只想根据每个单元格的值呈现索引2的列(即“更改”列)。 看看我的代码
private DefaultTableModel tmodel;
private final int table_colID_at_compID = 0;
private final int table_colID_at_compName = 1;
private final int table_colID_at_stockPresentageGrowth = 2;
private final int table_colID_at_compNetWorth = 3;
..........more code(unimportant code)..........
tmodel = new DefaultTableModel( new String[][] {} ,new String[]{"Comp. ID","Com. Name","Change %","Net Worth"} );
table = new JTable(tmodel){
public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){
Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);
if(columnIndex == table_colID_at_stockPresentageGrowth) {
double value = new Double(getValueAt(rowIndex, columnIndex).toString());
if( value < 0 )
componenet.setBackground(Color.RED);
else if( value == 0)
componenet.setBackground(Color.WHITE);
else
componenet.setBackground(Color.GREEN);
}
return componenet;
}
};
table_colID_at_<something>
的所有变量代表表列ID。图片:( imgur有错误)
答案 0 :(得分:0)
对于每隔一列,您必须将颜色设置为白色。