• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:CellEditorのBorderを変更
#navi(../)
RIGHT:Posted by [[aterai]] at 2005-08-22
#tags()
RIGHT:Posted by &author(aterai); at 2005-08-22
*CellEditorのBorderを変更 [#ia1fb931]
JTableのCellEditorにBorderを設定します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTIiyFXk2I/AAAAAAAAAS8/Dgu1EqDMma4/s800/CellEditorBorder.png)

**サンプルコード [#t48217b5]
#code{{
JTextField tf1 = new JTextField();
tf1.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
table.setDefaultEditor(Object.class, new DefaultCellEditor(tf1));

JTextField tf2 = new JTextField();
tf2.setHorizontalAlignment(JTextField.RIGHT);
tf2.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
table.setDefaultEditor(Integer.class, new DefaultCellEditor(tf2) {
  public boolean stopCellEditing() {
    Object o = getCellEditorValue();
    return (o==null)?false:super.stopCellEditing();
  }
  public Object getCellEditorValue() {
    Object o = super.getCellEditorValue();
    Integer iv;
    try{
      iv = Integer.valueOf(o.toString());
    }catch(NumberFormatException nfe) {
      iv = null;
    }
    return iv;
  }
});
#code(link){{
JTextField field = new JTextField();
field.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
table.setDefaultEditor(Object.class, new DefaultCellEditor(field));
}}

**解説 [#q9485122]
サンプルでは、DefaultTableModel#getColumnClassメソッドをオーバーライドして0列目はInteger.classを返すようにしています。Integer.classの場合はBorderが緑、それ以外の場合(Object.class)は赤で描画するようなデフォルトエディタをそれぞれ指定しています。
上記のサンプルでは、BorderFactory.createLineBorder(Color.RED, 2)を設定したJTextFieldを使用するDefaultCellEditorを作成し、JTable#setDefaultEditor(...)で、Object.classのデフォルトエディタとして設定しています。

以下のようにJTable#getColumnClassをオーバーライドしても同様です。
----
JTable#setDefaultEditor(...)を使用せずに、JTable#prepareEditor(...)をオーバーライドして、セルエディタとして使用するコンポーネントの背景色やBorderを変更することもできます。
#code{{
table = new JTable(sorter) {
  public Class<?> getColumnClass(int column) {
    if(column==0) {
      return Integer.class;
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 Object.class;
    return c;
  }
};
}}

デフォルトレンダラーの設定にも、getColumnClassが返すクラスが影響するので、それぞれレンダラーを設定する必要があります。
#code{{
TestRenderer tr = new TestRenderer();
table.setDefaultRenderer(Object.class,  tr);
table.setDefaultRenderer(Integer.class, tr);
}}
**参考リンク [#ydf6ae4b]
- [[JTableが使用するBooleanCellEditorの背景色を変更>Swing/BooleanCellEditor]]

//**参考リンク
**コメント [#q0b2f0c8]
- DefaultTableModel#getColumnClassとJTable#getColumnClassで動作が異なると勘違い((コンパイルし忘れていたみたいです))して解説していたのを修正しました。 -- [[aterai]] &new{2005-08-22 (月) 16:53:30};
- 0列目を実際に編集してソートするとClassCastExceptionが発生するバグがあります。 -- [[aterai]] &new{2005-08-22 (月) 17:07:47};
-- 修正するのを綺麗サッパリ忘れていました…。修正しました。 -- [[aterai]] &new{2007-02-15 (木) 16:34:10};
//- DefaultTableModel#getColumnClassとJTable#getColumnClassで動作が異なると勘違い((コンパイルし忘れていたみたいです))して解説していたのを修正しました。 -- [[aterai]] &new{2005-08-22 (月) 16:53:30};
//- 0列目を実際に編集してソートするとClassCastExceptionが発生するバグがあります。 -- [[aterai]] &new{2005-08-22 (月) 17:07:47};
//-- 修正するのを綺麗サッパリ忘れていました…。修正しました。 -- [[aterai]] &new{2007-02-15 (木) 16:34:10};

#comment