• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the value of the first selected item in a JComboBox

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The GUI app below should calculate(upon clicking the "Calculate" button) the total price of specific item bought by entering the quantity, price and by specifying if it is taxable or not. If it is taxable, 15% of the price will be charged.

Here is a code snippet of it:





By default, "Taxable" is the selected option in the JComboBox. The app calculates the total price correctly when the Non Taxable option is selected but displays 0.00 when "Taxable" is selected.

Here is a screenshot of GUI when the option Taxable is selected:
gui.png
[Thumbnail for gui.png]
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Don't use "==" to compare objects.

Instead you should be using the equals(...) method.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used .equals() but it didn't work

 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't actually ask a question so I'll assume your question is something like, "why does the taxable price show up as zero when I was expecting nonzero?" Allow me mention some things:

1) Does your taxAmt variable have a a non-zero value? (Also, would you normally expect to be under 1.0 or not under 1.0?)

2) I agree with Ms. Green that you should generally not use == to compare Strings, even if that's not related to your problem here.

3) Given that your GUI has a calculate button, I'm somewhat surprised to see an ItemListener in your code. Do you want things to happen when the user clicks calculate, or when the user manipulates the combo box, or both?
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I declared the taxAmt variable as such:



Actually, when the user clicks on the 'Calculate' button, it should display a message dialog displaying the total price based on the option of Taxable or Non Taxable selected, the price entered and quantity.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually, when the user clicks on the 'Calculate' button,....



Well the code in your ActionListener doesn't do the calculation. It assumes the total value has already been calculated.

So fix the code in your ActionListener to do the calculation. Then you can do basic debugging to make sure all the values of your variables are correct.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have solved it!
The prob in not getting the default selected item in the JComboBox is due to the fact I was using ItemStateChange(). Since when I select "Taxable"(the default selected item in the JComboBox), there is no change in selected item, it won't trigger or register anything in the ItemStateChange() for the part. Therefore, I changed it to ActionPerformed() and added that code snippet in the event handling for the button.

Here is the final code:



Thank you for all the replies
reply
    Bookmark Topic Watch Topic
  • New Topic