jp.
jp.

Reputation: 1

How to set number of selectable JRadioButton

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! :)

Upvotes: 0

Views: 747

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117665

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);
            }
        });
    }
}

Upvotes: 3

Jean-Paul
Jean-Paul

Reputation: 21180

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.

Upvotes: 1

Related Questions