3

I have a group of RadioButtons in a JPanel. I want them to have a green background when not selected, and a red background when one is selected. By the very nature of JRadioButtons, only one should be red at a single time.

My problem is that setting the background in an ActionListener does not work since it doesn't update the remaining buttons.

Is there any way I can iterate over elements of the ButtonGroup? (The method getElements doesn't seem to be what I need.)

Here is an SsCcE:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setPreferredSize(new Dimension(1024, 768));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel content = new JPanel();

                // This is the important stuff. :)

                ButtonGroup group = new ButtonGroup();
                for (int i = 0; i < 5; i++) {
                    final JRadioButton btn = new JRadioButton(String.valueOf(i));
                    group.add(btn);
                    content.add(btn);
                    btn.setBackground(Color.green);
                    btn.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent event) {
                            if (btn.isSelected()) {
                                btn.setBackground(Color.red);
                            } else {
                                btn.setBackground(Color.green);
                            }
                        }
                    });
                }

                // The important stuff is over. :(

                frame.setContentPane(content);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }
}
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • Duplicate: http://stackoverflow.com/questions/201287/how-do-i-get-which-jradiobutton-is-selected-from-a-buttongroup, apologies. – sdasdadas Jun 11 '13 at 16:12
  • 2
    What makes you say that `getElements()` is not what you need? – femtoRgon Jun 11 '13 at 16:36
  • @femtoRgon I'm not sure - I think maybe I misused it as an `Iterable`. Thanks for the heads up! – sdasdadas Jun 11 '13 at 16:42
  • 1
    @sdasdadas `getElements` returns an `Enumeration` of the elements in the `ButtonGroup`. I would say that this is exactly what you want. – Guillaume Polet Jun 11 '13 at 16:53
  • 1
    @sdasdadas [look at Select Button Group by Darryl Burke](http://tips4java.wordpress.com/2008/11/09/select-button-group/) – mKorbel Jun 11 '13 at 18:25

0 Answers0