質問

I thought the best way to demonstrate this is with a compilable example. I want to have a group of radio buttons which behave as such (only one can be selected at a time); however, when the currently selected radio button is 'selected' again, the selection is cleared (like a checkbox I guess).

My implementation checks the status of the radio button and, if selected, clears the selection (emulating the 'un-selecting' like a checkbox). The problem is, the radio button's select state changes before the ActionEvent fires, hence isSelected() returns true regardless of whether it was already selected. One solution would be to essentially log the ButtonGroup's selected button right before any ActionEvent fires, although my program isn't psychic like that :( I suspect I could easily implement this using a MouseListener, although that restricts functionality to mouse-usage, and I could use the keyboard, etc :) Thanks for any pointers!

Demo:

package sandbox;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class Sandbox {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        final ButtonGroup btns = new ButtonGroup();
        final JRadioButton btn1 = new JRadioButton("Button 1");
        final JRadioButton btn2 = new JRadioButton("Button 2");
        btns.add(btn1);
        btns.add(btn2);
        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() instanceof JRadioButton) {
                    btns.clearSelection();
                }
            }

        };
        btn1.addActionListener(al);
        btn2.addActionListener(al);
        f.setLayout(new FlowLayout());
        f.add(btn1);
        f.add(btn2);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

}
役に立ちましたか?

解決

you need combine item listener and action listener to achieve this, by oberseving the event sequence, if the "action event" occur after the "item event", that is the time to clear selection of button group.

Following code works for me.

public class JRadioButtonTest {

public static void main(String[] args) {
    JFrame f = new JFrame();
    final ButtonGroup btns = new ButtonGroup();
    final JRadioButton btn1 = new JRadioButton("Button 1");
    final JRadioButton btn2 = new JRadioButton("Button 2");
    btns.add(btn1);
    btns.add(btn2);
    EventAdapter ea = new EventAdapter(btns);
    btn1.addActionListener(ea);
    btn2.addActionListener(ea);
    btn1.addItemListener(ea);
    btn2.addItemListener(ea);

    f.setLayout(new FlowLayout());
    f.add(btn1);
    f.add(btn2);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

public static class EventAdapter implements ActionListener, ItemListener
{
    private ButtonGroup bg;

    boolean itemStateChanged = false;

    public EventAdapter(ButtonGroup bg)
    {

        this.bg = bg;
    }


    @Override
    public void itemStateChanged(ItemEvent itemEvent) {
        itemStateChanged = true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
            if (!itemStateChanged)
            {
                System.out.println("UnSelected");
                bg.clearSelection();
            }
        itemStateChanged = false;
    }

}

}

他のヒント

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top