Marius Katinas
Marius Katinas

Reputation: 470

Getting JCheckBox selected box value

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

Upvotes: 4

Views: 45712

Answers (2)

JB Nizet
JB Nizet

Reputation: 691765

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
}

Upvotes: 8

Robin
Robin

Reputation: 36611

If you store the checkboxes (e.g. in a List) you can loop over them and query their selected state when the OK button is pressed.

To obtain the String from the checkbox, you could opt to use the putClientProperty and getClientProperty methods, as explained in the class javadoc of JComponent

Upvotes: 2

Related Questions