Swing/CellEditorBorder のバックアップの現在との差分(No.10)
- category: swing folder: CellEditorBorder title: CellEditorのBorderを変更 tags: [JTable, TableCellEditor, Border] author: aterai pubdate: 2005-08-22T08:08:31+09:00 description: JTableのCellEditorにBorderを設定します。 description: Borderを変更したJTextFieldを、JTableのデフォルトセルエディタとして設定します。 image:
概要
JTable
のCellEditor
にBorder
を設定します。
概要
Border
を変更したJTextField
を、JTable
のデフォルトセルエディタとして設定します。
Screenshot
Advertisement
サンプルコード
サンプルコード
JTextField field = new JTextField();
field.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
table.setDefaultEditor(Object.class, new DefaultCellEditor(field));
View in GitHub: Java, Kotlin解説
上記のサンプルでは、BorderFactory.createLineBorder(Color.RED, 2)
を設定したJTextField
を使用するDefaultCellEditor
を作成し、JTable#setDefaultEditor(...)
で、Object.class
のデフォルトエディタとして設定しています。
解説
上記のサンプルでは、LineBorder
を設定したJTextField
を使用するDefaultCellEditor
を作成し、JTable#setDefaultEditor(...)
メソッドでObject
クラスのデフォルトセルエディタとして設定しています。
- -
JTable#setDefaultEditor(...)
を使用せずに、JTable#prepareEditor(...)
をオーバーライドして、セルエディタとして使用するコンポーネントの背景色やBorder
を変更することもできます。
-
JTable#setDefaultEditor(...)
を使用せずにJTable#prepareEditor(...)
をオーバーライドしてセルエディタとして使用するコンポーネントの背景色やBorder
を変更する方法もある
JTable table = new JTable(model) {
@Override public Component prepareEditor(
TableCellEditor editor, int row, int column) {
TableCellEditor editor, int row, int column) {
Component c = super.prepareEditor(editor, row, column);
if(c instanceof JCheckBox) {
JCheckBox b = (JCheckBox)c;
if (c instanceof JCheckBox) {
JCheckBox b = (JCheckBox) c;
b.setBorderPainted(true);
b.setBackground(getSelectionBackground());
}else if(c instanceof JComponent && convertColumnIndexToModel(column)==1) {
((JComponent)c).setBorder(
} else if (c instanceof JComponent
&& convertColumnIndexToModel(column) == 1) {
((JComponent) c).setBorder(
BorderFactory.createLineBorder(Color.GREEN, 2));
}
return c;
}
};