Getting JCheckBox selected box value

Marius Katinas picture Marius Katinas · Nov 17, 2012 · Viewed 38.1k times · Source

I want to ask is there a way to get information from JCheckBox without actionListener. In my code I scan a file of strings and each line has data which, if selected, should be added to an array in my program. Problem is that i will never know how many JCheckBoxes I will have, it depends from file.

So, my question is how to put selected strings to an array (or list) with a press of a button (ok) so i could do something else with them (in my case i need to get data from file or from hand input and put it in a red-black tree, so I will need to push selected strings to my putDataInTheTree method).

EDIT: Also, is it possible not to show those JCheckBoxes that already has been added to the program? I.E. if i choose fluids, next time I call input method fluids wont show in my panel?

Thanks in advance!

How it looks:

enter image description here

My code is so far:

public void input() {
    try {
        mainWindow.setEnabled(false);
        fromFile = new JFrame("Input from file");
        fromFile.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        fromFile.setLayout(new BorderLayout());
        fromFile.setSize(300,200);
        panelFromFile = new JPanel();
        panelFromFile.setLayout(new java.awt.GridLayout(0,1));
        JScrollPane scrollPane2 = new JScrollPane(panelFromFile); 
        scrollPane2.setMaximumSize(new Dimension(300, 180));

        FileReader File = new FileReader(data);
        BufferedReader Buffer = new BufferedReader(File); 
        while ((info = Buffer.readLine()) != null) {
            if (info != null) {
                JCheckBox check = new JCheckBox(info);
                panelFromFile.add(check);
            }
        }
        ok = new JButton("ok");
        ok.addActionListener(this);
        fromFile.add(scrollPane2, BorderLayout.CENTER);
        fromFile.add(ok, BorderLayout.SOUTH);
        fromFile.setLocationRelativeTo(null);
        fromFile.setResizable(false);
        fromFile.setVisible(true);
    }
    catch(Exception e) {
        text.append("Error in INPUT method");
        text.append(System.getProperty("line.separator"));
    }
}

Answer

JB Nizet picture JB Nizet · Nov 17, 2012

Add your checkboxes to a collection, and when the button is pressed, iterate through the checkboxes and get the text associated with each checked checkbox:

private List<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
...
    while ((info = Buffer.readLine()) != null) {
        if (info != null) {
            JCheckBox check = new JCheckBox(info);
            panelFromFile.add(check);
            this.checkBoxes.add(check);
        }
    }

...
public void actionPerformed(ActionEvent e) {
    List<String> infos = new ArrayList<String>();
    for (JCheckBox checkBox : checkBoxes) {
        if (checkBox.isSelected() {
            infos.add(checkBox.getText());
        }
    }
    // TODO do something with infos
}