1

I've a swing with some 50 check boxes, and a sample code for 3 is below.

JCheckBox checkboxOne = new JCheckBox("One");
JCheckBox checkboxTwo = new JCheckBox("Two");
JCheckBox checkboxThree = new JCheckBox("Three");


// add these check boxes to the container...

// add an action listener
ActionListener actionListener = new ActionHandler();
checkboxOne.addActionListener(actionListener);
checkboxTwo.addActionListener(actionListener);
checkboxThree.addActionListener(actionListener);
 
// code of the action listener class
 
class ActionHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
        JCheckBox checkbox = (JCheckBox) event.getSource();
        if (checkbox == checkboxOne) {
            System.out.println("Checkbox #1 is clicked");
        } else if (checkbox == checkboxTwo) {
            System.out.println("Checkbox #2 is clicked");
        } else if (checkbox == checkboxThree) {
            System.out.println("Checkbox #3 is clicked");
        }
    }
}

Here i want to loop through the 50 checkboxes like creating an ArrayList of the available checkboxes and loop them to check which is checked. I'm unable to understand on how to create a ArrayList of checkboxes.

I referred to Array of checkboxes in java, but i'm unable to understand how do i use it?

Please let me know how can do this.

Community
  • 1
  • 1
user3872094
  • 3,269
  • 8
  • 33
  • 71
  • What exactly don't you understand in the answer you linked to? If you successfully created the arraylist, you could simply do something like this to see which ones are checked: `for (JCheckBox cb : checkBoxes) {if (cb.isSelected()) {// What to do if it is selected}}` – Lukas Rotter Nov 26 '15 at 13:45
  • Hi @LuxxMiner, I've tried even this i used `for (JCheckBox cb : checkboxes) {if (cb.isSelected()) {System.out.println("selected");} else {System.out.println("no");}}`, but my console prints `no` for three, though i've checked and then hit the button – user3872094 Nov 26 '15 at 13:56
  • You have modified your code and made a new question on top of this one. This is not how you're supposed to do. If you have a new question, make a new question. The point of editing is to refine or show the progress of the question. Now, users searching for the same original question as you will find a non-sense answer. **I have an answer to your new question. Please post as a new one and rollback this one to it's original scope** – mr.celo Nov 26 '15 at 16:49

3 Answers3

4

Create an ArrayList of JCheckBox and add them in order. Then, you can use the indexOf() function to retrieve the number, like so:

public class TestFrame extends JFrame {

    public TestFrame() {
        setLayout(new GridLayout());
        setSize(500, 500);

        JCheckBox checkboxOne = new JCheckBox("One");
        JCheckBox checkboxTwo = new JCheckBox("Two");
        JCheckBox checkboxThree = new JCheckBox("Three");

        final ArrayList<JCheckBox> checkBoxes = new ArrayList<>();

        add(checkboxOne);
        add(checkboxTwo);
        add(checkboxThree);

        checkBoxes.add(checkboxOne);
        checkBoxes.add(checkboxTwo);
        checkBoxes.add(checkboxThree);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                JCheckBox checkbox = (JCheckBox) event.getSource();
                int index = checkBoxes.indexOf(checkbox) + 1;
                System.out.println("Checkbox #" + index + " is clicked");
            }
        };
        checkboxOne.addActionListener(actionListener);
        checkboxTwo.addActionListener(actionListener);
        checkboxThree.addActionListener(actionListener);
    }

    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Please note that this is adaptation of your code. This example was made as close as possible to your code so that the only modifications present are supposed to reflect the point I'm trying to get across.

Edit

Since you modified your question and a new one was made, here goes the second part of the answer:

and in my action listener, I was trying to get the checked boxes values, but it is throwing null as name and though I've checked, the output shows as not selected.

Modify your code to use getText() instead of getName(), such as:

JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        System.out.println(checkBoxes.size());
        for (int i = 0; i < checkBoxes.size(); i++) {
            if (checkBoxes.get(i).isSelected()) {
                System.out.println(" Checkbox " + i + " and " + checkBoxes.get(i).getText() + " is selected");
            } else {
                System.out.println(
                        " Checkbox " + i + " and " + checkBoxes.get(i).getText() + " is noooooot selected");
            }
        }
    }

});
mr.celo
  • 585
  • 1
  • 5
  • 17
2

In order to define an ArrayList with CheckBoxes please refer to following example:

List<JCheckBox> chkBoxes = new ArrayList<JCheckBox>();

Add your JCheckBox elements to the ArrayList using standard approach, for example:

JCheckBox chkBox1 = new JCheckBox();
chkBoxes.add(chkBox1);

Interatve over the list and carry out check if selected using JCheckBox method #.isSelected() as follows:

for(JCheckBox chkBox : chkBoxes){
   chkBox.isSelected(); // do something with this!
}
e.doroskevic
  • 2,129
  • 18
  • 25
0

If you need to get all checkboxes from actual existing Frame / Panel, you can use getComponents() method and one by one deside if it's checkbox (not sure if getComponents is supported by all containers)

eg.:

Component[] comps = jScrollPane.getComponents();
ArrayList<JCheckBox> chckBoxes= new ArrayList<JCheckBox>();


for(Component comp : comps) {
     if(comp instanceof JCheckBox) {
          chckBoxes.add((JCheckBox) comp);
     }
}

(Founded @ Get all swing components in a container )

Community
  • 1
  • 1
xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37