Swing/ComboCellRenderer のバックアップ差分(No.10)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ComboCellRenderer へ行く。
- 1 (2007-08-04 (土) 15:02:37)
- 2 (2010-11-01 (月) 23:09:02)
- 3 (2010-11-02 (火) 14:23:32)
- 4 (2010-11-02 (火) 20:28:32)
- 5 (2011-03-24 (木) 16:27:36)
- 6 (2013-02-09 (土) 23:47:36)
- 7 (2015-01-07 (水) 16:21:01)
- 8 (2016-06-07 (火) 14:44:35)
- 9 (2016-09-06 (火) 12:54:07)
- 10 (2017-10-14 (土) 17:56:22)
- 11 (2019-04-25 (木) 17:34:11)
- 12 (2021-02-03 (水) 01:12:38)
- 13 (2024-03-14 (木) 12:16:02)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: ComboCellRenderer title: JTableのCellRendererにJComboBoxを設定 tags: [JTable, JComboBox, TableCellRenderer, TableCellEditor] author: aterai pubdate: 2007-03-19 pubdate: 2007-03-19T02:32:35+09:00 description: JTableのCellRendererとしてJComboBoxを使用します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJ35Po_yI/AAAAAAAAAVE/z4Jn6Mv7-pc/s800/ComboCellRenderer.png --- * 概要 [#summary] `JTable`の`CellRenderer`として`JComboBox`を使用します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJ35Po_yI/AAAAAAAAAVE/z4Jn6Mv7-pc/s800/ComboCellRenderer.png) * サンプルコード [#sourcecode] #code(link){{ class ComboCellRenderer extends JComboBox implements TableCellRenderer { private static final Color ec = new Color(240, 240, 255); private final JTextField editor; public ComboCellRenderer() { super(); setEditable(true); setBorder(BorderFactory.createEmptyBorder()); editor = (JTextField) getEditor().getEditorComponent(); editor.setBorder(BorderFactory.createEmptyBorder()); editor.setOpaque(true); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { removeAllItems(); if (isSelected) { editor.setForeground(table.getSelectionForeground()); editor.setBackground(table.getSelectionBackground()); } else { editor.setForeground(table.getForeground()); editor.setBackground((row % 2 == 0) ? ec : table.getBackground()); } addItem(Objects.toString(value, "")); return this; } } }} * 解説 [#explanation] 上記のサンプルでは、`1`列目(中央)のセルの表示を`JComboBox`にするために、これを継承するセルレンダラーを設定しています。`1`列目はセルエディタも`JComboBox`ですが、これらは同じ`JComboBox`のインスタンスではなく、別々に用意しています。 レンダラーで使用する`JComboBox`は、セルの表示のみに使用するため、以下のように設定しています。 - 表示用のアイテム(文字列)を一つだけ持つ - 編集可にして`EditorComponent`の背景色などを他のセルと同様になるように変更 - セル内にきれいに収まるように余白を`0`に設定 * 参考リンク [#reference] - [[JTableのCellEditorにJComboBoxを設定>Swing/ComboCellEditor]] * コメント [#comment] #comment - セルの幅を大きくするとセルの表示が消えますが・・・これは一体なんでしょうか? -- &user(ichikawa); &new{2010-11-01 (月) 23:09:02}; -- ご指摘ありがとうございます。リサイズなどでセルの表示がおかしくなるのは、バグです。`DefaultTableCellRenderer#invalidate()`などと同じ(パフォーマンス上の理由)にするため、何もしないようにオーバーライドした、テスト中のコードとサンプルを誤ってアップロードしていました。 -- &user(aterai); &new{2010-11-02 (火) 14:23:32}; #comment