JTable の行全体の色を変更したい。
JTable を次のように定義しました。
JTable table = new JTable(data, columnNames);
ここで、data、columnNames は文字列テーブルです。
これを行う最も一般的な方法は、独自のクラスを作成することです。
public class StatusColumnCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
//Cells are by default rendered as a JLabel.
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
//Get the status for the current row.
l.setBackground(Color.GREEN);
//Return the JLabel which renders the cell.
return l;
}
}
そして呼び出します:
this.table.getColumnModel().getColumn(0).setCellRenderer(new StatusColumnCellRenderer());
しかし、それはうまくいきません。私は何を間違っていますか?