• category: swing folder: BooleanCellEditor title: JTableが使用するBooleanCellEditorの背景色を変更 tags: [JTable, TableCellEditor, JCheckBox] author: aterai pubdate: 2010-09-06T11:51:11+09:00 description: JTableがデフォルトで使用するBooleanCellEditorの背景色を選択色に変更します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIJ0rZk-I/AAAAAAAAASU/JvYohArvFpU/s800/BooleanCellEditor.png

概要

概要

JTableがデフォルトで使用するBooleanCellEditorの背景色を選択色に変更します。

サンプルコード

サンプルコード

JTable table = new JTable(model) {
  @Override public Component prepareEditor(TableCellEditor editor, int row, int column) {
  @Override public Component prepareEditor(
        TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    if (c instanceof JCheckBox) {
      JCheckBox b = (JCheckBox) c;
      b.setBackground(getSelectionBackground());
      b.setBorderPainted(true);
    }
    return c;
  }
};
View in GitHub: Java, Kotlin

解説

  • 上:デフォルト
    • セルをクリックして編集状態になるとCellEditorとして、背景色が白のJCheckBoxが表示される
  • 下:JTable#getSelectionBackground()
    • BooleanCellEditorとして使用するJCheckBoxの背景色が常にJTable#getSelectionBackground()になるようにJTable#prepareEditor(...)をオーバーライド

解説

  • 上: デフォルト
    • セルをクリックして編集状態になるとCellEditorとして背景色がJTableと同じ白色になるJCheckBoxが表示される
  • 下: JTable#getSelectionBackground()
    • BooleanCellEditorとして使用するJCheckBoxの背景色が常にJTable#getSelectionBackground()になるようにJTable#prepareEditor(...)メソッドをオーバーライド

コメント

参考リンク

JTable#prepareEditor(...) (Java Platform SE 8)

コメント