Swing/BooleanCellEditor のバックアップの現在との差分(No.1)
TITLE:JTableが使用するBooleanCellEditorの背景色を変更
Posted by terai at 2010-09-06
JTableが使用するBooleanCellEditorの背景色を変更
JTableがデフォルトで使用するBooleanCellEditorの背景色を選択色に変更します。- 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:
概要
JTable
がデフォルトで使用するBooleanCellEditor
の背景色を選択色に変更します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
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)c).setBackground(getSelectionBackground());
if (c instanceof JCheckBox) {
JCheckBox b = (JCheckBox) c;
b.setBackground(getSelectionBackground());
b.setBorderPainted(true);
}
return c;
}
};
解説
- 上:デフォルト
- セルをクリックして編集状態になるとCellEditorとして、背景色が白のJCheckBoxが表示される
- 下:JTable#getSelectionBackground()
- BooleanCellEditorとして使用するJCheckBoxの背景色が常にJTable#getSelectionBackground()になるようにJTable#prepareEditor(...)をオーバーライド
解説
- 上: デフォルト
- セルをクリックして編集状態になると
CellEditor
として背景色がJTable
と同じ白色になるJCheckBox
が表示される
- セルをクリックして編集状態になると
- 下:
JTable#getSelectionBackground()
-
BooleanCellEditor
として使用するJCheckBox
の背景色が常にJTable#getSelectionBackground()
になるようにJTable#prepareEditor(...)
メソッドをオーバーライド
-