Swing/CheckBoxesInTableCell のバックアップの現在との差分(No.20)
-
category: swing
folder: CheckBoxesInTableCell
title: JTableのCellにJCheckBoxを複数配置する
tags: [JTable, JCheckBox, TableCellRenderer, TableCellEditor, JPanel, InputMap, ActionMap]
author: aterai
pubdate: 2011-02-28T15:07:56+09:00
description: JTableのセル中にJCheckBoxを複数個配置します。
image:
hreflang:
href: https://java-swing-tips.blogspot.com/2011/03/checkboxes-in-jtable-cell.html lang: en
概要
概要
JTable
のセル中にJCheckBox
を複数個配置します。
Screenshot
Advertisement
サンプルコード
サンプルコード
class CheckBoxesPanel extends JPanel {
private static final String OSNAME = System.getProperty("os.name");
protected final String[] title = {"r", "w", "x"};
public JCheckBox[] buttons;
public CheckBoxesPanel() {
super();
setOpaque(false);
setBackground(new Color(0, 0, 0, 0));
setBackground(new Color(0x0, true));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
initButtons();
}
#spanadd
#spanend
protected void initButtons() {
buttons = new JCheckBox[title.length];
for (int i = 0; i < buttons.length; i++) {
JCheckBox b = new JCheckBox(title[i]);
b.setOpaque(false);
b.setFocusable(false);
b.setRolloverEnabled(false);
b.setBackground(new Color(0, 0, 0, 0));
b.setBackground(new Color(0x0, true));
buttons[i] = b;
add(b);
add(Box.createHorizontalStrut(5));
}
}
private static final String OSNAME = System.getProperty("os.name");
#spanadd
#spanend
protected void updateButtons(Object v) {
if ("Windows 7".equals(OSNAME)) { //Windows aero?
removeAll();
initButtons();
}
Integer i = (Integer)(v==null?0:v);
buttons[0].setSelected((i&(1<<2))!=0);
buttons[1].setSelected((i&(1<<1))!=0);
buttons[2].setSelected((i&(1<<0))!=0);
Integer i = (Integer) (v == null ? 0 : v);
buttons[0].setSelected((i & (1 << 2)) != 0);
buttons[1].setSelected((i & (1 << 1)) != 0);
buttons[2].setSelected((i & (1 << 0)) != 0);
}
}
#spandel
View in GitHub: Java, Kotlin#spanend
class CheckBoxesRenderer extends CheckBoxesPanel
implements TableCellRenderer, Serializable {
public CheckBoxesRenderer() {
super();
setName("Table.cellRenderer");
}
#spanadd
#spanend
@Override public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
updateButtons(value);
return this;
}
public static class UIResource extends CheckBoxesRenderer
implements UIResource{}
#spanadd
#spanend
public static class UIResource extends CheckBoxesRenderer implements UIResource {}
}
#spandel
#spanend
class CheckBoxesEditor extends CheckBoxesPanel
implements TableCellEditor, Serializable {
public CheckBoxesEditor() {
ActionListener al = new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
};
ActionMap am = getActionMap();
for (int i = 0; i < buttons.length; i++) {
final JCheckBox b = buttons[i];
b.addActionListener(al);
am.put(title[i], new AbstractAction(title[i]) {
@Override public void actionPerformed(ActionEvent e) {
b.setSelected(!b.isSelected());
fireEditingStopped();
}
});
}
InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), title[0]);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), title[1]);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, 0), title[2]);
}
#spanadd
#spanend
@Override public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
updateButtons(value);
return this;
}
#spanadd
#spanend
@Override public Object getCellEditorValue() {
int i = 0;
if (buttons[0].isSelected()) i |= 1 << 2;
if (buttons[1].isSelected()) i |= 1 << 1;
if (buttons[2].isSelected()) i |= 1 << 0;
return i;
}
//Copied from AbstractCellEditor
// Copied from AbstractCellEditor
protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
//......
// ...
解説
上記のサンプルでは、JTable
のセル内に3
つのJCheckBox
を配置したJPanel
を作成し、これをCellRenderer
とCellEditor
として別々に使用しています。JCheckBox
をマウスでクリックすると、そのJCheckBox
の選択状態だけが変化します。
解説
-
JTable
のセル内に3
つのJCheckBox
を配置したJPanel
を作成- この
JPanel
をCellRenderer
用とCellEditor
用として別々に使用
- この
-
JCheckBox
をマウスでクリックすると直下にあるJCheckBox
の選択状態のみ更新 - 編集中にカラムヘッダの移動やリサイズ(
JFrame
などのリサイズに連動)などが発生してもチェックした内容がリセットされないようにCellEditor
のチェックボックスがクリックされたらfireEditingStopped()
メソッドを呼び出して編集を終了し更新を確定する
- -
ヘッダカラムの移動、リサイズ(
JFrame
などのリサイズ)で、チェックした内容が消えてしまわないように、CellEditor
のチェックボックスがクリックされたらfireEditingStopped()
メソッドを呼び出して編集を終了し更新を確定するようにしています。
JTable
自体に以下の様なMouseListener
を追加してチェックボックスがクリックされるたびにtable.getCellEditor(row, col).stopCellEditing();
を呼び出しています。