1

I'm doing a project (something like a restaurant) for collage, and I'm not very good in Java (they didn't really try to teach us). The principle of the project is to show a message saying what radio buttons and check boxes I've selected.

I have 5 radio buttons, they are for selecting a meal, and 5 check boxes for selecting a side dish. I've managed to have the radio buttons work but I don't know how to make the check boxes work...

This is the code for the radio buttons (this goes for all 5):

 private void jRadioButton1ItemStateChanged(java.awt.event.ItemEvent evt) {
if (evt.getSource().equals(jRadioButton1)) { Meal= jRadioButton1.getText(); //Meal is a String }

I tried the same code for the check boxes but it only shows one side dish in the message, even though I selected multiple...

The code for the button that shows the message:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane, "You have chosen:\n" + String.valueOf(Meal) + "\n" + String.valueOf(SideDish)); }

So basically, if anyone is willing to help, please tell me how to make the check boxes work... that every selected check box is shown in the message, like this:

You have chosen:
Pizza //meal
Ketchup //selected side dish #1
Chilli peppers //selected side dish #2
Feta cheese //selected side dish #3

I hope my question is clear...

Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
Alejandra Uzelac
  • 647
  • 1
  • 7
  • 8
  • Since the checkboxes can be multiple checked, I would save them in a kind of vector or list. Then, if you click the button, you just iterate over them and call something like 'if(yourList.elementAt(i).isChecked) // getText() and store the string somewhere' – Valentino Ru Jun 19 '12 at 15:53
  • @ValentinoRu as Attila has answered, the Set is a better collection for this kind of work: no copies, single instance of the items, just using `add` and `remove` methods (no need to add more logic). – Luiggi Mendoza Jun 19 '12 at 15:55
  • agree, unfortunately I read it just now after posting an example answer with a vector. – Valentino Ru Jun 19 '12 at 16:00

4 Answers4

3

Since you want to display text for zero or more checkboxes, you need to check for each whether it is selected, and concatenate the resulting text, instead of keeping only one option. Moreover, since check boxes can be checked and unchecked independently, it may be better to check their state only at the moment when the button is pressed instead of attempting to keep track of them all the time. E.g.:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

  SideDish = "";
  if (jCheckBox1.getState()))
  {
    SideDish += jCheckBox1.getText();
  }
  ...
  if (jCheckBox5.getState()))
  {
    SideDish += ", " + jCheckBox5.getText();
  }

  JOptionPane.showMessageDialog(rootPane, "You have chosen:\n" + Meal + "\n" + SideDish);
}

This is just an illustration, which won't always display separating commas properly - I will leave the fix to you as an exercise :-)

A more elegant solution would be to use a collection of Strings to collect the side dishes - again, you may try improving the code towards this.

Btw, you don't need String.valueOf() to print Strings so I removed it from the code above. And the Java coding convention is to start variable/field names with lowercase.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Concatenate the results is not a good solution, what if the user unchecks an option? – Luiggi Mendoza Jun 19 '12 at 15:54
  • @LuiggiMendoza - then that checkbox's `getState()` will return false, and you will not concatenate that option – Attila Jun 19 '12 at 15:57
  • @LuiggiMendoza, note that this code is inside the event handler of Button1. Thus it always checks for the *current* state of each checkbox, at the moment the button is pressed. – Péter Török Jun 19 '12 at 15:58
  • @Attila I check option 1 and 2, then uncheck both and check option 3, the concatenated String will have Option1, Option2, Option3. Correct me if I'm wrong. – Luiggi Mendoza Jun 19 '12 at 16:00
  • @LuiggiMendoza, yes you are wrong. At the time of pressing Button1, only Option3 will be checked so Side dish #3 will be the only one printed. – Péter Török Jun 19 '12 at 16:02
  • Oh you were posting the button click. I though you were implementing the checkbox click. Now another question, why make it in the button click, isn't harder to maintain in case that you add or remove checkboxes from the GUI? – Luiggi Mendoza Jun 19 '12 at 16:06
  • 1
    @LuiggiMendoza, this is just an illustration so don't expect it to be perfect :-) One could add all checkboxes to a collection and iterate through the collection to make it more maintainable though. – Péter Török Jun 19 '12 at 16:12
  • I'm working in Netbeans, and when I type the code for: if (jCheckBox1.getState())) the getState doesn't show up, only getSelectedIcon(), getSelectedObjects(), getSize() and getSize(Dimension rv) ... – Alejandra Uzelac Jun 19 '12 at 16:21
  • When I do type getState(), I get an error saying: cannot find symbol symbol: method getState() location: jCheckBox1 of type javax.swing.JCheckBox Why?? – Alejandra Uzelac Jun 19 '12 at 16:52
  • Ok, I figured it out... I should've chosen the AWT check boxes, then the getState() works, and the getText() I had to replace with getLabel() and now it works! So thank you very much! – Alejandra Uzelac Jun 19 '12 at 17:26
2

As you can select multiple checkboxes at the same time, you should collect the checked values in a collection. Set<String> comes to mind as you can select one side dish only once.

Also, you did not have this problem as each selection of the radio button overwrote the previous one, but you will need to remove items from the set if their corresponding checkbox is unticked.

Note: Set<> is an interface, you will need an implementing class to actually use it. In your case a HashSet<> would work (see documentation for the available method for Set and HashSet)

When you display the choice of side dishes, you can enumerate the elements in the set and eother print them one-by-one or collect the result in a string by concatenating the elements.

Note: you might not evenneed the set if you have direct access to the checkboxes: at the time of displaying which side dish is chosen, just check the state of each checkbox and accumulate the choices string as outlined above

Attila
  • 28,265
  • 3
  • 46
  • 55
1

Here is an example

Lets say you have 4 sidedishes

Vector<JCheckBox> boxes = new Vector<JCheckBox>
boxes.add(checkbox1) .... .add(checkbox4);

and on clicking the button

Vector<String> sideDishes = new Vector<String>();
for(int i=0; i<boxes.size(); i++){
   if(boxes.elementAt(i).isSelected(){
      sideDishes.add(boxes.elementAt(i).getText();
   }
}
Valentino Ru
  • 4,964
  • 12
  • 43
  • 78
0

I am thinking that first you should learn is how to analyze problem and how to use Google or other search engine sites, So try to read this docs and look to examples

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45