0

How would I go about setting the number of selectable items of JRadioButtons? I tried adding the radiobuttons to a buttongroup, and overriding the buttongroup class, but cant figure which method to modify.

Basically, I want to allow selection of only two radiobuttons. I am aware this is possible using checkboxes, but I need the "roudness" of the radiobuttons, and figure this should be an easier way to go, instead of modifying the look and feel of the checkbox.

Thanks a bunch! :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
jp.
  • 1
  • 3
  • 2
    whats will be happens in the case two (restricted to two) are isSelected and user to click to the 3rd. – mKorbel Jul 16 '13 at 19:07
  • I agree with @mKorbel, which of the two selected buttons should be unselected when a third radio button is selected? – BackSlash Jul 16 '13 at 19:09
  • You could set the non-selected radiobuttons to `setEnabled(false)` if you want a visual cue that there can be no more selections. Can fire that off in your listener pretty trivially, re-enabling same thing. Otherwise, we'll need more info on what you want to happen. – roippi Jul 16 '13 at 19:11
  • Ideally, all the other radiobuttons would be greyed out, using the suggested setEnabled() method, until the user removes one of the selected buttons, and would then be able to select another (by enabling the others again). – jp. Jul 16 '13 at 19:15
  • 1
    But, as I see radio buttons, this is not the expected behavior for them. If I click a radio button, it expect it will be selected (and maybe other radio buttons will be deselected), but never deselected. – Jean Waghetti Jul 16 '13 at 19:31
  • That is true, I would never suspect that clicking a radio button would disable it. So your options are to provide an external "clear" button, or to use checkboxes. Or to just have the two selections locked in with no way to change them, I guess. – roippi Jul 16 '13 at 19:35
  • What I meant was, if the user has 2 radiobuttons selected, to be able to select a third, he would have to remove one of the previous selected ones. – jp. Jul 16 '13 at 19:40

2 Answers2

3

Here is an example:

enter image description here

package com.haraj.test.java;

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedList;
import java.util.Queue;

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

public class JRadioButtonTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel contentPane = (JPanel) frame.getContentPane();
                contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
                contentPane.setLayout(new GridLayout());

                final Queue<JRadioButton> selectedButtons = new LinkedList<JRadioButton>();
                ItemListener listener = new ItemListener()
                {
                    @Override
                    public void itemStateChanged(ItemEvent e)
                    {
                        JRadioButton newButton = (JRadioButton) e.getSource();

                        if(e.getStateChange() == ItemEvent.DESELECTED) selectedButtons.remove(newButton);
                        else
                        {
                            if(selectedButtons.size() == 2)
                            {
                                JRadioButton oldButton = selectedButtons.poll();
                                if(oldButton != newButton) oldButton.setSelected(false);
                            }
                            selectedButtons.add(newButton);
                        }
                    }
                };

                JRadioButton[] buttons = new JRadioButton[6];
                for(int i = 0; i < buttons.length; i++)
                {
                    buttons[i] = new JRadioButton();
                    buttons[i].addItemListener(listener);
                    contentPane.add(buttons[i]);
                }

                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

One way would be to add an ActionListener to each individual radiobutton which updates a counter if the button is selected.

You can read about jRadioButton functions HERE.

You can then do a function if the counter hits two which makes the other buttons grey (unclickable) using:

    .setActionCommand("disable");

You can find more info about the possible methods in the API.

Jean-Paul
  • 19,910
  • 9
  • 62
  • 88
  • Thank you for all your suggestions. Roippi's solution worked fine. Instead of using a ButtonGroup to limit the number of selectable radiobuttons, I created a custom itemListener that checked a list of selected radiobuttons, and disabled the others if two were simultaneosly selected. – jp. Jul 16 '13 at 19:46