TableCellEditorのレイアウトを変更
Total: 11891
, Today: 1
, Yesterday: 2
Posted by aterai at
Last-modified:
概要
TableCellEditor
のレイアウトを変更して、CellEditor
の隣にJButton
を配置します。
Screenshot
Advertisement
サンプルコード
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);
}
@Override public Component getTableCellEditorComponent(
JTable table, Object value,
boolean isSelected, int row, int column) {
// System.out.println("getTableCellEditorComponent");
field.setText(Objects.toString(value, ""));
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
field.requestFocusInWindow();
}
});
return panel;
}
@Override public boolean isCellEditable(final EventObject e) {
// System.out.println("isCellEditable");
if (e instanceof KeyEvent) {
// System.out.println("KeyEvent");
EventQueue.invokeLater(new Runnable() {
@Override 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);
}
@Override public Component getComponent() {
return panel;
}
}
View in GitHub: Java, Kotlin解説
0
列目DefaultCellEditor
を継承するCustomComponentCellEditor
を作成JTextField
をコンストラクタの引数にしているがこれは使用せず、実際のセルエディタはJPanel
で作成して使用TableCellEditor#getTableCellEditorComponent
がJPanel
を返す
- この
JPanel
のレイアウトをBorderLayout
にしてJTextField
とJButton
を配置 TableCellEditor#getCellEditorValue
はJTextField
の値を返し、フォーカス、キー入力時の編集開始などもJTextField
になるように変更- 参考: 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内にアイコンを追加
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.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, BUTTON_WIDTH));
field.addHierarchyListener(new HierarchyListener() {
@Override public void hierarchyChanged(HierarchyEvent e) {
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0
&& field.isShowing()) {
field.removeAll();
field.add(button);
Rectangle r = field.getBounds();
button.setBounds(r.width - BUTTON_WIDTH, 0, BUTTON_WIDTH, r.height);
}
}
});
}
@Override public Component getComponent() {
// @see JTable#updateUI()
SwingUtilities.updateComponentTreeUI(button);
return super.getComponent();
}
}
2
列目DefaultCellEditor
を継承するCustomComponentCellEditor2
を作成JTextField
をDefaultCellEditor
のコンストラクタの引数にしているがこれは使用せず、実際のセルエディタはJPanel
を継承するCustomComponent
で作成して使用
CustomComponent#processKeyBinding(...)
をオーバーライドしてキー入力開始時にKeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(field, e);
を呼び出している0
列目のCustomComponentCellEditor
と同様
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(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
if (!field.isFocusOwner() && !pressed) {
field.requestFocusInWindow();
EventQueue.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 component;
}
@Override public Component getComponent() {
return component;
}
}
3
列目- デフォルト