私のJavaプログラムでは、表のセルの色を設定する際に問題があります。 以下に示すように、セルには4つの異なるカラム成分があります。 これらのセルの色を変更すると、column1の色が変わります。JTableのセルの色を変更しているときの変更点
DefaultTableModel tableModel = new DefaultTableModel(columns,0){
@Override
public Class<?> getColumnClass(int column) {
switch(column) {
case 0: return String.class;
case 1: return ImageIcon.class;
case 2: return Integer.class;
case 3: return Integer.class;
default: return Object.class;
}
}
};
そして、私はそのようなセルの色を変更した:
table1.setDefaultRenderer(Object.class, new ColorChange);
// I guess Object.class causes the problem
public class ColorChange implements TableCellRenderer {
public final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
// Apply zebra style on table rows
if (row % 2 == 0) {
c.setBackground(Color.WHITE);
} else {
c.setBackground(Color.decode("#F8F8F8"));
}
return c;
}
}
そこで問題は、どのように私はすべてのカラムの色を変えることができるということですか?
ありがとうございます。
'(行%2 == 0){ c.setBackground(Color.WHITE);' Nimbus PLAFは、テーブルの行に代替色(自動的)を提供します。 –