Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getSelectedIndex JComboBox calculator  RSS feed

 
Eli Kook
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!