Yes No Cell Editor And Renderer For JTable


Here are two classes for a JTable cell for use as a cell renderer and cell editor that makes up two JRadioButtonJPanel inside a .

If you plan to add more JRadioButton objects in the container, feel free to do so but be aware that these JRadioButtons are displayed horizontally courtesy of the BoxLayout that I used. If you want them displayed vertically, just change the value of the axis of the BoxLayout to Y_AXIS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class YesNoRadioCellRenderer extends JPanel implements TableCellRenderer {
    private JRadioButton yes = new JRadioButton("Yes");
    private JRadioButton no = new JRadioButton("No");
    private ButtonGroup bg = new ButtonGroup();
 
    public YesNoRadioCellRenderer() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        bg.add(yes);
        bg.add(no);
        add(yes);
        add(no);
    }
 
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (value != null) {
            if (value.toString().equals("true")) bg.setSelected(yes.getModel(), true);
            else bg.setSelected(no.getModel(), true);
        }
        return this;
    }
 
}
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
public class YesNoRadioCellEditor extends JPanel implements TableCellEditor {
    private JRadioButton yes = new JRadioButton("Yes");
    private JRadioButton no = new JRadioButton("No");
    private ButtonGroup bg = new ButtonGroup();
 
    public YesNoRadioCellEditor() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        bg.add(yes);
        bg.add(no);
        add(yes);
        add(no);
    }
 
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)  {
        if (value != null) {
            if (value.toString().equals("true")) bg.setSelected(yes.getModel(), true);
            else bg.setSelected(no.getModel(), true);
        }
        return this;
    }
 
    @Override
    public Object getCellEditorValue() {
        return bg.getSelection().getActionCommand();
    }
 
    @Override
    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }
 
    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }
 
    @Override
    public boolean stopCellEditing() {
        return true;
    }
 
    @Override
    public void cancelCellEditing() { }
 
    @Override
    public void addCellEditorListener(CellEditorListener l) { }
 
    @Override
    public void removeCellEditorListener(CellEditorListener l) { }
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails

Leave a Reply

Spam protection by WP Captcha-Free