• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TableCellEditorのレイアウトを変更
#navi(../)
RIGHT:Posted by [[terai]] at 2009-02-02
*TableCellEditorのレイアウトを変更 [#d64f644d]
TableCellEditorのレイアウトを変更して、CellEditorの隣にJButtonを配置します。

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

#screenshot

**サンプルコード [#bcfb306a]
#code{{
class CustomCellEditor extends DefaultCellEditor {
  private static final int BUTTON_WIDTH = 20;
  protected final JButton button = new JButton();
  public CustomCellEditor(final JTextField field) {
    super(field);
    field.add(button);
    field.setBorder(BorderFactory.createEmptyBorder(0,2,0,BUTTON_WIDTH));
    field.addHierarchyListener(new HierarchyListener() {
      public void hierarchyChanged(HierarchyEvent e) {
        if((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED)!=0
            && field.isShowing()) {
          Rectangle r = field.getBounds();
          button.setBounds(r.width-BUTTON_WIDTH, 0, BUTTON_WIDTH, r.height);
        }
      }
    });
  }
}
}}
#code{{
class CustomComponentCellEditor extends AbstractCellEditor implements TableCellEditor {
  protected final JTextField field;
  protected JButton button;
  private final JPanel panel = new JPanel(new BorderLayout());
  public CustomComponentCellEditor() {
    super();
    field = new JTextField();
    button = new JButton();
    button.setPreferredSize(new Dimension(25, 0));
    field.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
    panel.add(field);
    panel.add(button, BorderLayout.EAST);
  }
  public Object getCellEditorValue() {
    return field.getText();
  }
  public Component getTableCellEditorComponent(JTable table, Object value,
                         boolean isSelected, int row, int column) {
    field.setText(value!=null?value.toString():"");
    return panel;
  }
  public boolean isCellEditable(final java.util.EventObject e) {
    if(e instanceof MouseEvent) {
      return ((MouseEvent)e).getClickCount()==2;
    }else if(e instanceof KeyEvent) {
      EventQueue.invokeLater(new Runnable() {
        public void run() {
          char kc = ((KeyEvent)e).getKeyChar();
          if(!Character.isIdentifierIgnorable(kc)) {
            field.setText(field.getText()+kc);
          }
          field.setCaretPosition(field.getText().length());
          field.requestFocusInWindow();
        }
      });
    }
    return true;
  }
}
}}

**解説 [#k40bb169]
-0列目
--JPanelをセルエディタに設定(TableCellEditor#getTableCellEditorComponentがJPanelを返す)
--パネルのレイアウトをBorderLayoutにして、JTextFieldとJButtonを配置
--TableCellEditor#getCellEditorValueはJTextFieldの値を返し、フォーカス、キー入力時の編集開始などもJTextFieldになるように変更
--参考: [[Swing - JTable editor issue>http://forums.sun.com/thread.jspa?messageID=10586784]]:Darryl.Burke さんの投稿(2009/01/27 20:12 (reply 6 of 8))

-1列目
--DefaultCellEditor(JTextField)をセルエディタとして使用
--JTextFieldにJButtonの幅の右余白を設定
--パネルのレイアウトをnullにして、JTextFieldが表示されたときに、余白にJButtonをsetBoundsで配置
--JTextFieldが表示されたときに、余白にJButtonをsetBoundsで配置
--参考:[[JTextField内にアイコンを追加>Swing/IconTextField]]

**参考リンク [#i9a92ff6]
-[[Swing - JTable editor issue>http://forums.sun.com/thread.jspa?messageID=10586784]]
-[[JTextField内にアイコンを追加>Swing/IconTextField]]

**コメント [#k839423b]
#comment