Swing/MultipleButtonsInTableCell のバックアップ(No.22)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/MultipleButtonsInTableCell へ行く。
- 1 (2009-10-06 (火) 11:55:06)
- 2 (2009-10-19 (月) 19:26:55)
- 3 (2009-11-03 (火) 04:36:55)
- 4 (2009-11-03 (火) 05:57:35)
- 5 (2011-03-04 (金) 15:57:52)
- 6 (2011-03-05 (土) 01:15:35)
- 7 (2011-03-10 (木) 02:33:21)
- 8 (2011-03-11 (金) 00:37:21)
- 9 (2011-05-06 (金) 18:49:11)
- 10 (2012-02-07 (火) 18:09:44)
- 11 (2012-09-28 (金) 19:36:52)
- 12 (2013-01-05 (土) 19:50:02)
- 13 (2013-05-24 (金) 11:57:26)
- 14 (2013-05-24 (金) 23:59:16)
- 15 (2013-07-21 (日) 02:37:44)
- 16 (2013-07-26 (金) 01:15:37)
- 17 (2013-08-17 (土) 01:12:15)
- 18 (2013-08-17 (土) 02:15:47)
- 19 (2014-05-15 (木) 18:18:57)
- 20 (2014-06-26 (木) 16:20:28)
- 21 (2014-11-07 (金) 03:09:24)
- 22 (2014-11-17 (月) 00:00:10)
- 23 (2014-11-21 (金) 18:29:32)
- 24 (2015-12-27 (日) 23:09:36)
- 25 (2017-06-15 (木) 15:23:24)
- 26 (2017-11-02 (木) 15:32:16)
- 27 (2018-02-24 (土) 19:51:30)
- 28 (2018-06-13 (水) 18:44:19)
- 29 (2020-06-07 (日) 15:57:34)
- 30 (2021-11-21 (日) 22:03:09)
- 31 (2023-05-20 (土) 01:55:25)
- title: JTableのセルに複数のJButtonを配置する tags: [JTable, TableCellEditor, TableCellRenderer, JButton, JPanel, ActionListener] author: aterai pubdate: 2009-10-05T12:57:02+09:00 description: JTableのセル内にクリック可能な複数のJButtonを配置します。
概要
JTable
のセル内にクリック可能な複数のJButton
を配置します。
Screenshot
Advertisement
サンプルコード
class ButtonsPanel extends JPanel {
public final List<JButton> buttons =
Arrays.asList(new JButton("view"), new JButton("edit"));
public ButtonsPanel() {
super();
setOpaque(true);
for (JButton b: buttons) {
b.setFocusable(false);
b.setRolloverEnabled(false);
add(b);
}
}
}
View in GitHub: Java, Kotlinclass ButtonsRenderer extends ButtonsPanel
implements TableCellRenderer {
public ButtonsRenderer() {
super();
setName("Table.cellRenderer");
}
@Override public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(isSelected?table.getSelectionBackground():table.getBackground());
return this;
}
}
class ButtonsEditor extends ButtonsPanel
implements TableCellEditor {
public ButtonsEditor(final JTable table) {
super();
//---->
//DEBUG: view button click -> control key down + edit button(same cell) press
// -> remain selection color
MouseListener ml = new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
ButtonModel m = ((JButton) e.getSource()).getModel();
if (m.isPressed() && table.isRowSelected(table.getEditingRow())
&& e.isControlDown()) {
setBackground(table.getBackground());
}
}
};
buttons.get(0).addMouseListener(ml);
buttons.get(1).addMouseListener(ml);
//<----
buttons.get(0).addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
fireEditingStopped();
JOptionPane.showMessageDialog(table, "Viewing");
}
});
buttons.get(1).addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
int row = table.convertRowIndexToModel(table.getEditingRow());
Object o = table.getModel().getValueAt(row, 0);
fireEditingStopped();
JOptionPane.showMessageDialog(table, "Editing: " + o);
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
fireEditingStopped();
}
});
}
@Override public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
return this;
}
@Override public Object getCellEditorValue() {
return "";
}
//Copied from AbstractCellEditor
//protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
@Override public boolean isCellEditable(java.util.EventObject e) {
return true;
}
//......
解説
上記のサンプルでは、CellRenderer
用とCellEditor
用に、JButton
を2
つ配置したJPanel
をそれぞれ作成しています。アクションイベントを設定するのは、CellEditor
用のJButton
で、CellRenderer
用のJButton
は表示のためのダミーです。
LookAndFeel
などが更新されたら、JTable#updateUI()
内でSwingUtilities#updateRendererOrEditorUI()
を呼び出すなどして、各セルレンダラーやセルエディタ(これらはJTable
の子コンポーネントではないので)を更新AbstractCellEditor
を継承するセルエディタは、Component
もDefaultCellEditor
も継承していないので、LookAndFeel
を変更しても追従しない- そのため、
JTable#updateUI()
をオーバーライドして、セルエディタ自体を作成し直すなどの対応が必要
- このサンプルでは、
Component
を継承(TableCellEditor
を実装)するセルエディタを作成し、AbstractCellEditor
から必要なメソッドをコピーして回避する方法を使用している
//SwingUtilities#updateRendererOrEditorUI()
static void updateRendererOrEditorUI(Object rendererOrEditor) {
if (rendererOrEditor == null) {
return;
}
Component component = null;
if (rendererOrEditor instanceof Component) {
component = (Component) rendererOrEditor;
}
if (rendererOrEditor instanceof DefaultCellEditor) {
component = ((DefaultCellEditor) rendererOrEditor).getComponent();
}
if (component != null) {
SwingUtilities.updateComponentTreeUI(component);
}
}
JSpinner
(2
つのJButton
とJTextField
の組み合わせ)をCellEditor
に使用する
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class ButtonsInsideCellTest {
private JComponent makeUI() {
String[] columnNames = {"Buttons", "Spinner"};
Object[][] data = {
{50, 100}, {100, 50}, {30, 20}, {0, 100}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
table.setRowHeight(36);
table.setAutoCreateRowSorter(true);
TableColumn column = table.getColumnModel().getColumn(0);
column.setCellRenderer(new ButtonsRenderer());
column.setCellEditor(new ButtonsEditor());
column = table.getColumnModel().getColumn(1);
column.setCellRenderer(new SpinnerRenderer());
column.setCellEditor(new SpinnerEditor());
return new JScrollPane(table);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ButtonsInsideCellTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class SpinnerPanel extends JPanel {
public final JSpinner spinner = new JSpinner(new SpinnerNumberModel(100, 0, 200, 1));
public SpinnerPanel() {
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.insets = new Insets(0, 10, 0, 10);
c.fill = GridBagConstraints.HORIZONTAL;
setOpaque(true);
add(spinner, c);
}
}
class SpinnerRenderer extends SpinnerPanel implements TableCellRenderer {
public SpinnerRenderer() {
super();
setName("Table.cellRenderer");
}
@Override public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
spinner.setValue((Integer) value);
return this;
}
}
class SpinnerEditor extends SpinnerPanel implements TableCellEditor {
// public SpinnerEditor(final JTable table, final int column) {
// super();
// }
@Override public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
spinner.setValue((Integer) value);
return this;
}
@Override public Object getCellEditorValue() {
//System.out.println("getCellEditorValue: " + spinner.getValue());
//try {
// spinner.commitEdit();
//} catch(Exception pe) {
// // Edited value is invalid, spinner.getValue() will return
// // the last valid value, you could revert the spinner to show that:
// JComponent editor = spinner.getEditor();
// if (editor instanceof JSpinner.DefaultEditor) {
// ((JSpinner.DefaultEditor) editor).getTextField().setValue(spinner.getValue());
// }
//}
return spinner.getValue();
}
//Copied from AbstractCellEditor
//protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
@Override public boolean isCellEditable(EventObject e) {
return true;
}
@Override public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override public boolean stopCellEditing() {
try {
spinner.commitEdit();
} catch(Exception pe) {
Toolkit.getDefaultToolkit().beep();
return false;
// 直前の値に戻して、編集を終了する場合
// Edited value is invalid, spinner.getValue() will return
// the last valid value, you could revert the spinner to show that:
//JComponent editor = spinner.getEditor();
//if (editor instanceof JSpinner.DefaultEditor) {
// ((JSpinner.DefaultEditor) editor).getTextField().setValue(spinner.getValue());
//}
}
fireEditingStopped();
return true;
}
@Override public void cancelCellEditing() {
fireEditingCanceled();
}
@Override public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
@Override public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listenerList.getListeners(CellEditorListener.class);
}
protected void fireEditingStopped() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) changeEvent = new ChangeEvent(this);
((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) changeEvent = new ChangeEvent(this);
((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
}
}
}
}
class ButtonsPanel extends JPanel {
public final List<JButton> buttons = Arrays.asList(new JButton("+"), new JButton("-"));
public final JLabel label = new JLabel() {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = 50;
return d;
}
};
public int i = -1;
public ButtonsPanel() {
super();
label.setHorizontalAlignment(SwingConstants.RIGHT);
setOpaque(true);
add(label);
for (JButton b: buttons) {
b.setFocusable(false);
b.setRolloverEnabled(false);
add(b);
}
}
}
class ButtonsRenderer extends ButtonsPanel implements TableCellRenderer {
public ButtonsRenderer() {
super();
setName("Table.cellRenderer");
}
@Override public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
this.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
label.setText(value != null ? value.toString() : "");
return this;
}
}
class ButtonsEditor extends ButtonsPanel implements TableCellEditor {
public ButtonsEditor() {
super();
buttons.get(0).addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
i++;
label.setText("" + i);
fireEditingStopped();
}
});
buttons.get(1).addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
i--;
label.setText("" + i);
fireEditingStopped();
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
fireEditingStopped();
}
});
}
@Override public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
i = (Integer) value;
label.setText("" + i);
return this;
}
@Override public Object getCellEditorValue() {
return i;
}
//Copied from AbstractCellEditor
//protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
@Override public boolean isCellEditable(EventObject e) {
return true;
}
@Override public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
@Override public void cancelCellEditing() {
fireEditingCanceled();
}
@Override public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
@Override public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listenerList.getListeners(CellEditorListener.class);
}
protected void fireEditingStopped() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i]==CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) changeEvent = new ChangeEvent(this);
((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i]==CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) changeEvent = new ChangeEvent(this);
((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
}
}
}
}
参考リンク
- JTableのセルにJButtonを追加して行削除
- JTableのセルにHyperlinkを表示
- Table Button Column « Java Tips Weblog
- JTableのセル中にJRadioButtonを配置
- JTableのCellにJCheckBoxを複数配置する