An attempt to add radio buttons to a JTable using a renderer and editor resulted in the inability to achieve exclusivity. The code snippet provided demonstrates the issue.
As an alternative, consider using a JComboBox as an editor for mutually exclusive choices within a row. This is a more efficient approach for space management and user experience.
// ... // Import declarations // ... public class JRadioAsRendererEditor extends JPanel { // ... // Class variables and methods // ... private class StatusRenderer extends StatusPanel implements TableCellRenderer { // ... // Overridden methods // ... @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setStatus((Status) value); if (isSelected) { setBackground(table.getSelectionBackground()); } else { setBackground(table.getBackground()); } return this; } } }
1. Alternative Renderer and Editor
You can create your own custom renderer and editor classes to achieve the desired behavior. Here's an example:
public class RadioCellRenderer extends DefaultTableCellRenderer { private RadioButtonGroup group; public RadioCellRenderer(RadioButtonGroup group) { this.group = group; } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JRadioButton button = (JRadioButton) value; button.setSelected(group.isSelected(button)); return button; } } public class RadioCellEditor extends DefaultCellEditor { private RadioButtonGroup group; public RadioCellEditor(JRadioButtonCheckBox box, RadioButtonGroup group) { super(box); this.group = group; } @Override public Object getCellEditorValue() { return delegate.get(); } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { JRadioButton button = (JRadioButton) value; if (group.isClearable()) { group.clearSelection(); } group.add(button); return button; } }
2. DefaultTableCellRenderer and DefaultCellEditor
You can use the DefaultTableCellRenderer and DefaultCellEditor classes with a custom JRadioButton that handles exclusivity. Here's an example:
public class ExclusiveJRadioButton extends JRadioButton { private RadioButtonGroup group; public ExclusiveJRadioButton(RadioButtonGroup group) { this.group = group; addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { group.setSelected(ExclusiveJRadioButton.this, true); } }); } @Override public void setSelected(boolean selected) { if (group.isSelected(this)) { return; } super.setSelected(selected); } }
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3