1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package unbbayes.gui.table;
22
23 import java.awt.Component;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.MouseEvent;
27 import java.util.EventObject;
28
29 import javax.swing.AbstractCellEditor;
30 import javax.swing.JRadioButton;
31 import javax.swing.JTable;
32 import javax.swing.table.TableCellEditor;
33
34
35
36
37 public class RadioButtonCellEditor extends AbstractCellEditor implements
38 TableCellEditor {
39
40 private static final long serialVersionUID = 1L;
41
42 JRadioButton radioButton;
43
44 protected int clickCountToStart = 1;
45
46 public RadioButtonCellEditor() {
47 radioButton = new JRadioButton();
48 radioButton.setHorizontalAlignment(JRadioButton.CENTER);
49 radioButton.addActionListener(new ActionListener() {
50 public void actionPerformed(ActionEvent e) {
51
52
53
54
55 if (!radioButton.isSelected())
56 cancelCellEditing();
57 stopCellEditing();
58 }
59 });
60 }
61
62 public Component getTableCellEditorComponent(JTable table,
63 Object value, boolean isSelected, int row, int col) {
64 radioButton.setSelected(((Boolean) value).booleanValue());
65 return radioButton;
66 }
67
68 public Component getComponent() {
69 return radioButton;
70 }
71
72 public int getClickCountToStart() {
73 return clickCountToStart;
74 }
75
76 public Object getCellEditorValue() {
77 return Boolean.valueOf(radioButton.isSelected());
78 }
79
80 public boolean isCellEditable(EventObject anEvent) {
81 if (anEvent instanceof MouseEvent)
82 return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
83 return true;
84 }
85
86 public boolean shouldSelectCell(EventObject anEvent) {
87 return true;
88 }
89
90 public boolean stopCellEditing(EventObject anEvent) {
91 fireEditingStopped();
92 return true;
93 }
94
95 public void cancelCellEditing() {
96 fireEditingCanceled();
97 }
98 }