• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableのセル中にJRadioButtonを配置
#navi(../)
RIGHT:Posted by [[aterai]] at 2009-12-21
*JTableのセル中にJRadioButtonを配置 [#xe89889f]
JTableのセル中に複数のJRadioButtonを配置します。[http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html JTableExamples2]を元に修正を行っています。
---
category: swing
folder: RadioButtonsInTableCell
title: JTableのセル中にJRadioButtonを配置
tags: [JTable, JRadioButton, TableCellRenderer, TableCellEditor, JPanel, ActionListener]
author: aterai
pubdate: 2009-12-21T00:36:55+09:00
description: JTableのセル中に複数のJRadioButtonを配置します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRX5e43uI/AAAAAAAAAhE/QX6qn9jFOB8/s800/RadioButtonsInTableCell.png
---
* 概要 [#summary]
`JTable`のセル中に複数の`JRadioButton`を配置します。%%[http://www2.gol.com/users/tame/swing/examples/JTableExamples2.html JTableExamples2]%%を元に修正を行っています。

-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRX5e43uI/AAAAAAAAAhE/QX6qn9jFOB8/s800/RadioButtonsInTableCell.png)

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTRX5e43uI/AAAAAAAAAhE/QX6qn9jFOB8/s800/RadioButtonsInTableCell.png)
* サンプルコード [#sourcecode]
#code(link){{
class RadioButtonsPanel extends JPanel {
  private final String[] answer = {
      Answer.A.toString(), Answer.B.toString(), Answer.C.toString()};
  public JRadioButton[] buttons;
  public ButtonGroup bg = new ButtonGroup();

**サンプルコード [#f5687e69]
#code{{
class RadioButtonsPanel extends JPanel {
  private final String[] answer = { "A", "B", "C" };
  public final JRadioButton[] buttons;
  public final ButtonGroup bg = new ButtonGroup();
  public RadioButtonsPanel() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    initButtons();
  }

  protected void initButtons() {
    bg = new ButtonGroup();
    buttons = new JRadioButton[answer.length];
    for(int i=0;i<buttons.length;i++) {
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new JRadioButton(answer[i]);
      buttons[i].setActionCommand(answer[i]);
      buttons[i].setFocusable(false);
      buttons[i].setRolloverEnabled(false);
      add(buttons[i]);
      bg.add(buttons[i]);
    }
  }

  protected void updateSelectedButton(Object v) {
    if("A".equals(v)) {
      buttons[0].setSelected(true);
    }else if("B".equals(v)) {
      buttons[1].setSelected(true);
    }else{
      buttons[2].setSelected(true);
    if (v instanceof Answer) {
      removeAll();
      initButtons();
      switch ((Answer) v) {
        case A:
          buttons[0].setSelected(true);
          break;
        case B:
          buttons[1].setSelected(true);
          break;
        case C:
          buttons[2].setSelected(true);
          break;
        default:
          break;
      }
    }
  }
}
}}
#code{{

class RadioButtonsRenderer extends RadioButtonsPanel implements TableCellRenderer {
  public RadioButtonsRenderer() {
    super();
    setName("Table.cellRenderer");
  }

  @Override public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    updateSelectedButton(value);
    return this;
  }
}
}}
#code{{

class RadioButtonsEditor extends RadioButtonsPanel implements TableCellEditor {
  public RadioButtonsEditor() {
    super();
    ActionListener al = new ActionListener() {
      @Override public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
      }
    };
    for(AbstractButton b: buttons) b.addActionListener(al);
    for (AbstractButton b: buttons) {
      b.addActionListener(al);
    }
  }

  @Override public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
    updateSelectedButton(value);
    return this;
  }

  @Override public Object getCellEditorValue() {
    return bg.getSelection().getActionCommand();
    // bug: return bg.getSelection().getActionCommand();
    return Answer.valueOf(bg.getSelection().getActionCommand());
  }

  //Copid from AbstractCellEditor
  //protected EventListenerList listenerList = new EventListenerList();
  // Copied from AbstractCellEditor
  // protected EventListenerList listenerList = new EventListenerList();
  transient protected ChangeEvent changeEvent = null;
//......
// ...
}}

**解説 [#rfb9e48f]
上記のサンプルでは、JRadioButtonを3つ配置したJPanelを、CellRenderer、CellEditor用に二つ用意しています。CellEditor内の各JRadioButtonには、クリックされたら編集を終了して更新をコミットするためのActionListenerを追加しています。
* 解説 [#explanation]
上記のサンプルでは、`JRadioButton`を`3`つ配置した`JPanel`を`CellRenderer`と`CellEditor`用に`2`つ用意しています。`CellEditor`内の各`JRadioButton`にはクリックされたら編集を終了して更新を`TableModel`にコミットするための`ActionListener`を追加しています。

%%実際にどのJRadioButtonがクリックされたかなどは、以下のように、JTableに追加したMouseListenerで処理するようになっています。%%
* 参考リンク [#reference]
- [https://tips4java.wordpress.com/2009/07/12/table-button-column/ Table Button Column ≪ Java Tips Weblog]
- [[JTableのセルに複数のJButtonを配置する>Swing/MultipleButtonsInTableCell]]
- [[JTableのCellにJCheckBoxを複数配置する>Swing/CheckBoxesInTableCell]]
- [[JTableのセルにJRadioButton>Swing/RadioButtonCellEditor]]

//#code{{
//table.addMouseListener(new MouseAdapter() {
//  @Override public void mouseReleased(MouseEvent e) {
//    JTable t = (JTable)e.getComponent();
//    Point pt = e.getPoint();
//    int row  = t.rowAtPoint(pt);
//    int col  = t.columnAtPoint(pt);
//    if(t.convertRowIndexToModel(row)>=0 && t.convertColumnIndexToModel(col)==1) {
//      TableCellEditor ce = t.getCellEditor(row, col);
//      //http://tips4java.wordpress.com/2009/07/12/table-button-column/
//      ce.stopCellEditing();
//      Component c = ce.getTableCellEditorComponent(t, null, true, row, col);
//      Point p = SwingUtilities.convertPoint(t, pt, c);
//      Component b = SwingUtilities.getDeepestComponentAt(c, p.x, p.y);
//      if(b instanceof JRadioButton) ((JRadioButton)b).doClick();
//    }
//  }
//});
//}}

**参考リンク [#xf10c31e]
-[http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html JTableExamples2]
-[http://tips4java.wordpress.com/2009/07/12/table-button-column/ Table Button Column ≪ Java Tips Weblog]
-[[JTableのセルに複数のJButtonを配置する>Swing/MultipleButtonsInTableCell]]
-[[JTableのCellにJCheckBoxを複数配置する>Swing/CheckBoxesInTableCell]]

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