1

So Im trying to check if the total number of checkboxes selected is == 10. If so I'm wanting to disable the rest of the checkboxes that aren't selected! Meaning that a user can only select 10 checkboxes.

I was thinking a for loop which loops through an array and then checks if a checkbox is selected and then adds 1 to the counter. If the counter reaches 10 I thought It may be easy to disable the rest of the checkboxes but obviously not!

          List<Checkbox> chkSongs = new ArrayList<Checkbox>();
            String labels[] = {"songA", "songB", "songC", "songD", "songE",
                "songF", "songG", "songH", "songI", "songJ", "songK", "songL", "songM",
            "songN", "songO", "songP", "songQ", "songR", "songS", "songT"}
            for (int i = 0; i < labels.length; i++) {
                Checkbox checkbox = new Checkbox(labels[i]);
                chkSongs.add(checkbox); 
                formPanel.add(checkbox);
            } 
James111
  • 15,378
  • 15
  • 78
  • 121

2 Answers2

1

Here is something I wrote on the fly, but I think it does what you want:

public class CheckBoxFrame extends JFrame {

    public CheckBoxFrame() throws HeadlessException {
        JPanel formPanel = new JPanel(new GridLayout(-1, 4));
        List<Checkbox> chkSongs = new ArrayList<>();
        String labels[] = {"songA", "songB", "songC", "songD", "songE",
                           "songF", "songG", "songH", "songI", "songJ", "songK", "songL", "songM",
                           "songN", "songO", "songP", "songQ", "songR", "songS", "songT"};
        for (String label : labels) {
            Checkbox checkbox = new Checkbox(label);
            chkSongs.add(checkbox);
            formPanel.add(checkbox);
        }
        CheckBoxListener listener = new CheckBoxListener(chkSongs, 10);
        setContentPane(formPanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public class CheckBoxListener implements ItemListener {

        private int boxesChecked;
        private final List<Checkbox> checkBoxes;
        private final int maxSelections;

        public CheckBoxListener(List<Checkbox> checkBoxes, int maxSelections) {
            this.checkBoxes = new ArrayList<>(Objects.requireNonNull(checkBoxes));
            this.maxSelections = maxSelections;
            this.boxesChecked = (int) this.checkBoxes.stream().filter(cb -> cb.getState()).count();
            this.checkBoxes.forEach(cb -> cb.addItemListener(this));
        }

        @Override
        public void itemStateChanged(ItemEvent ie) {
            if (ie.getStateChange() == ItemEvent.SELECTED) {
                boxesChecked++;
            } else {
                boxesChecked--;
            }

            if (boxesChecked == maxSelections) {
                checkBoxes.stream().filter(cb -> !cb.getState()).forEach(cb -> cb.setEnabled(false));
            } else if (boxesChecked == maxSelections - 1) {
                checkBoxes.stream().forEach(cb -> cb.setEnabled(true));
            }
        }

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CheckBoxFrame().setVisible(true);
            }

        });
    }

}

This still has its flaws (you could add the listener to another checkbox, for example), but it's not too bad.

Steffen
  • 3,999
  • 1
  • 23
  • 30
0

Use 'getState()' to see if a checkbox is checked.

boolean checked = checkbox.getState();
Identity1
  • 1,139
  • 16
  • 33