Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# getSelectedIndex JComboBox calculator

Eli Kook
Greenhorn
Posts: 27
Hello everyone,
I have a school assignment to change a GUI Calculator from JRadioButton to JComboBox in Array. I have made the change but I have some issue. For some reason when I choose to calculate from the JComboBox, it does not work and I need it to be calculated directly after choosing option from the JComboBox. I must use get selected Index method as a part of the assignment.
Any help will be much appreciated!
Here is my code:

Rob Camick
Ranch Hand
Posts: 2636
9
A combo box is just like a button. You need to add an ActionListener to the combo box if you want to do something when an item is selected.

Read the JComboBox API and you will find a link to the Swing tutorial on "How to Use Combo Boxes" for more information and examples.

Eli Kook
Greenhorn
Posts: 27
Thank you for your reply though I have googled the subject earlier before your post and looked also in the link you have sent me, I have still not found the mistake in my code and that is why I have posted this add in order to receiving some sort of explaining or lead to where is my mistake in the code.

Brett Spell
Ranch Hand
Posts: 118
The reason only division works is because you've defined a series of if statements in succession starting on line #160 with nothing that causes only one of them to be executed. If you debug through it you'll see that the "if division" block always gets executed, so if you've selected a different operation it will generate the error message (fehlerFlag gets set to true).

What you really need is to execute only one of a set of mutually exclusive blocks, so look into using a switch statement instead of a series of if statements.

Eli Kook
Greenhorn
Posts: 27
Thank you for your replay, I have found the solution: I need to add if else as you can see in the above code but now only one last thing is open.
As soon as I add the second number, I need the first calculation the addition to be executed, how can I do that?

Eli Kook
Greenhorn
Posts: 27
I mean as soon as I write a number in the Zahl2 I want it to do the addition. Is it possible? and if yes, how?