TITLE:TableCellEditorのレイアウトを変更
#navi(../)
RIGHT:Posted by [[aterai]] at 2009-02-02
*TableCellEditorのレイアウトを変更 [#d64f644d]
TableCellEditorのレイアウトを変更して、CellEditorの隣にJButtonを配置します。

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

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTIlcF-6vI/AAAAAAAAATA/mS6Q_BfuY6c/s800/CellEditorLayout.png)

**サンプルコード [#bcfb306a]
#code{{
class CustomComponentCellEditor extends DefaultCellEditor {
  protected final JTextField field;
  protected JButton button;
  private final JPanel panel = new JPanel(new BorderLayout());
  public CustomComponentCellEditor(JTextField field) {
    super(field);
    this.field = field;
    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);
    panel.setFocusable(false);
  }
  public Component getTableCellEditorComponent(JTable table, Object value,
                         boolean isSelected, int row, int column) {
    //System.out.println("getTableCellEditorComponent");
    field.setText(value!=null?value.toString():"");
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        field.requestFocusInWindow();
      }
    });
    return panel;
  }
  public boolean isCellEditable(final java.util.EventObject e) {
    //System.out.println("isCellEditable");
    if(e instanceof KeyEvent) {
      //System.out.println("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 super.isCellEditable(e);
  }
}
}}

**解説 [#k40bb169]
-0列目
--DefaultCellEditorを継承するCustomComponentCellEditorを作成
--JTextFieldをコンストラクタの引数にしているが、ダミー
--実体はJPanelで、これをセルエディタとして表示(TableCellEditor#getTableCellEditorComponentがJPanelを返す)
--このJPanelのレイアウトをBorderLayoutにして、JTextFieldとJButtonを配置
--TableCellEditor#getCellEditorValueはJTextFieldの値を返し、フォーカス、キー入力時の編集開始などもJTextFieldになるように変更
--参考: [http://forums.sun.com/thread.jspa?messageID=10586784 Swing - JTable editor issue]:Darryl.Burke さんの投稿(2009/01/27 20:12 (reply 6 of 8))

-1列目
--DefaultCellEditorを継承するCustomCellEditorを作成
--JTextFieldをコンストラクタの引数(セルエディタの実体)として使用
--JTextFieldにJButtonの幅の右余白を設定
--JTextFieldが表示されたときに、余白にJButtonをsetBoundsで配置
--参考:[[JTextField内にアイコンを追加>Swing/IconTextField]]

#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);
        }
      }
    });
  }
}
}}

-2列目
--DefaultCellEditorを継承するCustomComponentCellEditor2を作成
--JTextFieldをコンストラクタの引数にしているが、ダミー
--実体はJPanelを継承するCustomComponentで、これをセルエディタとして表示
--CustomComponent#processKeyBinding(...)をオーバーライドして、キー入力開始時にKeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(field, e);を呼び出している
--あとは、0列目のCustomComponentCellEditorと同様

#code{{
class CustomComponent extends JPanel {
  public final JTextField field = new JTextField();
  protected JButton button;
  public CustomComponent() {
    super(new BorderLayout(0,0));
    button = new JButton();
    this.add(field);
    this.add(button, BorderLayout.EAST);
  }
  @Override protected boolean processKeyBinding(
      final KeyStroke ks, final KeyEvent e, int condition, boolean pressed) {
    if(!field.isFocusOwner() && !pressed) {
      field.requestFocusInWindow();
      SwingUtilities.invokeLater(new Runnable() {
        @Override public void run() {
          KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .redispatchEvent(field, e);
        }
      });
    }
    return super.processKeyBinding(ks, e, condition, pressed);
  }
}
class CustomComponentCellEditor2 extends DefaultCellEditor {
  private final CustomComponent component;
  public CustomComponentCellEditor2(CustomComponent component) {
    super(component.field);
    this.component = component;
  }
  @Override public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
    component.field.setText(value!=null?value.toString():"");
    return this.component;
  }
}
}}

-3列目
--デフォルト

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

**コメント [#k839423b]
- F2で編集開始した場合、フォーカスできない問題を修正。 -- [[aterai]] &new{2009-08-10 (月) 15:50:27};
- ソースコードだけ変更して、こちらのページの修正を忘れていたorzので、新しく追加した2列目の解説と、最終列はデフォルトであることを追記。スクリーンショットは面倒なので更新しない。 -- [[aterai]] &new{2011-08-30 (火) 18:26:59};

#comment