Bonjour à tous j'aimerais créer un jtable avec une colonne de type bouton radio contenu dans un groupe de bouton radio merci d'avance.
Version imprimable
Bonjour à tous j'aimerais créer un jtable avec une colonne de type bouton radio contenu dans un groupe de bouton radio merci d'avance.
Salut,
Il y a un exemple d'une telle JTable sur www.java2s.com / Radio Button Table Example.
Merci bien voilà ce que sa donne:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 import java.awt.Component; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ButtonGroup; import javax.swing.DefaultCellEditor; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIDefaults; import javax.swing.UIManager; import javax.swing.event.TableModelEvent; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; class RadioButtonRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value == null) return null; return (Component) value; } } class RadioButtonEditor extends DefaultCellEditor implements ItemListener { private JRadioButton button; public RadioButtonEditor(JCheckBox checkBox) { super(checkBox); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if (value == null) return null; button = (JRadioButton) value; button.addItemListener(this); return (Component) value; } public Object getCellEditorValue() { button.removeItemListener(this); return button; } public void itemStateChanged(ItemEvent e) { super.fireEditingStopped(); } } public class JRadioButtonTableExample extends JFrame { public JRadioButtonTableExample() { super("JRadioButtonTable Example"); UIDefaults ui = UIManager.getLookAndFeel().getDefaults(); UIManager.put("RadioButton.focus", ui.getColor("control")); DefaultTableModel dm = new DefaultTableModel(); dm.setDataVector(new Object[][] { { "Group 1", new JRadioButton("A") }, { "Group 1", new JRadioButton("B") }, { "Group 1", new JRadioButton("C") }, { "Group 2", new JRadioButton("a") }, { "Group 2", new JRadioButton("b") } }, new Object[] { "String", "JRadioButton" }); JTable table = new JTable(dm) { public void tableChanged(TableModelEvent e) { super.tableChanged(e); repaint(); } }; ButtonGroup group1 = new ButtonGroup(); group1.add((JRadioButton) dm.getValueAt(0, 1)); group1.add((JRadioButton) dm.getValueAt(1, 1)); group1.add((JRadioButton) dm.getValueAt(2, 1)); ButtonGroup group2 = new ButtonGroup(); group2.add((JRadioButton) dm.getValueAt(3, 1)); group2.add((JRadioButton) dm.getValueAt(4, 1)); table.getColumn("JRadioButton").setCellRenderer( new RadioButtonRenderer()); table.getColumn("JRadioButton").setCellEditor( new RadioButtonEditor(new JCheckBox())); JScrollPane scroll = new JScrollPane(table); getContentPane().add(scroll); setSize(200, 140); setVisible(true); } public static void main(String[] args) { JRadioButtonTableExample frame = new JRadioButtonTableExample(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }