I am having trouble figuring out how to do the following. I am trying to enable the user to check the books he wants using my checklist, but I cannot figure out how to save his selections (the checked books) in order to determine the price he has to pay. Here is my checklist code:
for(int k=0;k<catalogue.getCatalogue().size();k++)
{
frame.add(new JCheckBox(catalogue.cat.get(k).toString()));
}
frame.setLayout(new FlowLayout());
frame.setSize(900,900);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);}});
It just shows the list of books (catalogue is an Arraylist containing the books, which I read from a file) with a checkbox beside it. I need help in figuring out how to "save" his selections so that I can store it in another arraylist of books.
You should use an Array/ArrayList of JCheckLists. For example:
JCheckBox[] checkboxes = new JCheckBox[catalogue.getCatalogue().size()];
And then:
for(int k=0;k<catalogue.getCatalogue().size();k++)
{
checkboxes[k] = new JCheckBox(catalogue.cat.get(k).toString()));
frame.add(checkboxes[k]);
}
Now you can easily check the state of each checkbox, for example, you can reference the first one by checkboxes[0]