Home > Java > javaTutorial > body text

How to Achieve Exclusivity When Adding Radio Buttons to JTable Groups?

Patricia Arquette
Release: 2024-11-12 06:43:02
Original
256 people have browsed it

How to Achieve Exclusivity When Adding Radio Buttons to JTable Groups?

Adding Radio Buttons to Groups in a JTable

Problem Statement

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.

Alternative Solution

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.

Code Example

// ...
// 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;
        }
    }
}
Copy after login

Additional Alternatives

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;
    }
}
Copy after login

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);
    }
}
Copy after login

The above is the detailed content of How to Achieve Exclusivity When Adding Radio Buttons to JTable Groups?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template