Swing/CellEditorBorder のバックアップの現在との差分(No.5)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CellEditorBorder へ行く。
- 1 (2005-08-22 (月) 08:08:31)
- 2 (2005-09-16 (金) 12:33:58)
- 3 (2006-02-27 (月) 15:31:19)
- 4 (2006-04-12 (水) 19:35:52)
- 5 (2007-02-15 (木) 16:30:37)
- 6 (2007-04-10 (火) 00:21:58)
- 7 (2008-06-20 (金) 13:36:20)
- 8 (2011-05-24 (火) 02:11:46)
- 9 (2013-03-27 (水) 16:36:20)
- 10 (2014-11-27 (木) 01:34:47)
- 11 (2015-01-22 (木) 21:17:10)
- 12 (2016-07-26 (火) 16:01:10)
- 13 (2017-09-27 (水) 19:06:59)
- 14 (2019-03-29 (金) 19:13:46)
- 15 (2021-01-07 (木) 16:47:00)
- 16 (2023-07-07 (金) 13:52:49)
- 17 (2023-11-16 (木) 14:54:55)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:CellEditorのBorderを変更 #navi(../) *CellEditorのBorderを変更 [#ia1fb931] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2005-08-22~ 更新日:&lastmod; --- category: swing folder: CellEditorBorder title: CellEditorのBorderを変更 tags: [JTable, TableCellEditor, Border] author: aterai pubdate: 2005-08-22T08:08:31+09:00 description: Borderを変更したJTextFieldを、JTableのデフォルトセルエディタとして設定します。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIiyFXk2I/AAAAAAAAAS8/Dgu1EqDMma4/s800/CellEditorBorder.png --- * 概要 [#summary] `Border`を変更した`JTextField`を、`JTable`のデフォルトセルエディタとして設定します。 #contents #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIiyFXk2I/AAAAAAAAAS8/Dgu1EqDMma4/s800/CellEditorBorder.png) **概要 [#j484283d] JTableのCellEditorにBorderを設定します。 * サンプルコード [#sourcecode] #code(link){{ JTextField field = new JTextField(); field.setBorder(BorderFactory.createLineBorder(Color.RED, 2)); table.setDefaultEditor(Object.class, new DefaultCellEditor(field)); }} #screenshot * 解説 [#explanation] 上記のサンプルでは、`LineBorder`を設定した`JTextField`を使用する`DefaultCellEditor`を作成し、`JTable#setDefaultEditor(...)`メソッドで`Object`クラスのデフォルトセルエディタとして設定しています。 **サンプルコード [#t48217b5] JTextField tf1 = new JTextField(); tf1.setBorder(BorderFactory.createLineBorder(Color.red, 2)); table.setDefaultEditor(Object.class, new DefaultCellEditor(tf1)); JTextField tf2 = new JTextField(); tf2.setBorder(BorderFactory.createLineBorder(Color.green, 2)); tf2.setHorizontalAlignment(JTextField.RIGHT); table.setDefaultEditor(Integer.class, new DefaultCellEditor(tf2)); - `JTable#setDefaultEditor(...)`を使用せずに`JTable#prepareEditor(...)`をオーバーライドしてセルエディタとして使用するコンポーネントの背景色や`Border`を変更する方法もある -&jnlp; -&jar; -&zip; #code{{ JTable table = new JTable(model) { @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.setBorderPainted(true); b.setBackground(getSelectionBackground()); } else if (c instanceof JComponent && convertColumnIndexToModel(column) == 1) { ((JComponent) c).setBorder( BorderFactory.createLineBorder(Color.GREEN, 2)); } return c; } }; }} **解説 [#q9485122] サンプルでは、DefaultTableModel#getColumnClassメソッドをオーバーライドして0列目はInteger.classを返すようにしています。Integer.classの場合はBorderが緑、それ以外の場合(Object.class)は赤で描画するようなデフォルトエディタをそれぞれ指定しています。 * 参考リンク [#reference] - [[JTableが使用するBooleanCellEditorの背景色を変更>Swing/BooleanCellEditor]] 以下のようにJTable#getColumnClassをオーバーライドしても同様です。 table = new JTable(sorter) { public Class getColumnClass(int column) { if(column==0) { return Integer.class; } return Object.class; } }; デフォルトレンダラーの設定にも、getColumnClassが返すクラスが影響するので、それぞれレンダラーを設定する必要があります。 TestRenderer tr = new TestRenderer(); table.setDefaultRenderer(Object.class, tr); table.setDefaultRenderer(Integer.class, tr); //**参考リンク **コメント [#q0b2f0c8] - DefaultTableModel#getColumnClassとJTable#getColumnClassで動作が異なると勘違い((コンパイルし忘れていたみたいです))して解説していたのを修正しました。 -- [[terai]] &new{2005-08-22 (月) 16:53:30}; - 0列目を実際に編集してソートするとClassCastExceptionが発生するバグがあります。 -- [[terai]] &new{2005-08-22 (月) 17:07:47}; * コメント [#comment] #comment #comment