-1

I have a JRadioButton and I have to set the selected item into 12, then after that it will disable/grey out the other selections if it already reaches into 12.

All I know is that if you add the JRadioButton into the ButtonGroup, that will set the selected item into 1, but not multiple which is what I am aiming for.

Is this possible? Is there any methods/way on how to do it? Thank you for any of your suggestions :)

roeygol
  • 4,908
  • 9
  • 51
  • 88
pep
  • 43
  • 6

2 Answers2

2

Create an arraylist of JRadioButtons. Every time the user clicks on a JRadioButton (is enabled) go through the list and count how many JRadioButtons have been enabled. If the count is greater than or equal to 12, disable all other radioButtons until the user unselects one.

This is just one of the many ways to go about this,

Hope this helps.

//initiate jradiobutton arraylist (this will be a field at top of class)
buttons = new ArrayList<JRadioButtons>();

//Create buttons with a listener attached
JRadioButton b1 = new JRadioButton("RadioButton One");
b1.setActionListener(myActionListener);
b1.setActionCommand("select");
buttons.add(b1);

//Add rest of buttons in the same way
JRadioButton b2...

//Add the radio buttons to your panel and such

Now when the user clicks on one of your buttons, your actionlistener will trigger, here you can do your check for the amount enabled

public void actionPerformed(ActionEvent e){
    //Check if action was a jradiobutton
    if(e.getActionCommand().equals("select")){

        int count = 0;
        //Here check the amount of buttons selected
        for(JRadioButton button: buttons){
            if(button.isSelected()) count++;
        }

        //Now check if count is over 12
        if(count > 12){
            for(JRadioButton button: buttons){
                 //if the button trying to activate when 12 already have been, disable it
                 if(button.equals(e.getSource()) button.setSelcted(false);
            }
        }
    }
}

This should disable buttons when already selected and also only allow the user to select 12 of the buttons in the arraylist.

Kyal Bond
  • 296
  • 1
  • 12
  • as answer to you question in the comments, you could do the same what you have written in the answer with `JCheckBox` – Blip Oct 20 '16 at 08:34
  • oh i'm really sorry if i'm asking too much but can you give me just a little bit of code? i'm a beginner in using swing and all i know are the very basic ones. thank you so much :) – pep Oct 20 '16 at 08:50
  • No problem, there are a lot of other things to take into account aswell, but this was as simple as i could get it. – Kyal Bond Oct 20 '16 at 09:34
  • @KyalBond: the downside of your solution ist that that the user does not see that Buttons are disabled unless she clicks on them. – Timothy Truckle Oct 20 '16 at 09:40
2

JRadioButton is the wrong type since the user expects to select one only. You better use JCheckBox with an custom ActionListener like in this SSCCE:

    public class CheckBoxActivationTest {
        public static void main(String[] args) {

            final int MAX_ACTIVE_CHECK_BOXES = 12;
            List<JCheckBox> allCheckBoxes = new ArrayList<>();

            ActionListener actionListener = new ActionListener() {
                private int activeCheckBoxesCounter = 0;
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("action!");
                    JCheckBox currentCheckBox = (JCheckBox) e.getSource();
                    activeCheckBoxesCounter += currentCheckBox.isSelected() ? 1 : -1;
                    for (JCheckBox jCheckBox : allCheckBoxes) {
                        jCheckBox.setEnabled(jCheckBox.isSelected()
                                || MAX_ACTIVE_CHECK_BOXES > activeCheckBoxesCounter);
                    }
                }
            };

            JPanel jPanel = new JPanel(new GridLayout(6, 0));
            for (int i = 0; i < 30; i++) {
                JCheckBox checkBox = new JCheckBox("Option "+(1+ i));
                allCheckBoxes.add(checkBox);
                checkBox.addActionListener(actionListener);
                jPanel.add(checkBox);
            }
            JOptionPane.showMessageDialog(null, jPanel);
        }
    }
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51