Solved

how to determine which option in a combo box is selected

Posted on 2007-04-08
6
223 Views
Last Modified: 2010-03-31
How do I determine which option in a combo box is selected?  MY code is below

I want to be able to say if 1 dollar is selected when you press deposit bill, enter 1 dollar, five dollars enter five.


thanks

billComboBox = new JComboBox();
            billComboBox.setToolTipText("Please select your bill to enter then press deposit bill");
            billComboBox.setModel(new DefaultComboBoxModel(new String[] {"1 Dollar", "5 Dollars"}));

            JButton depositBillButton;
            depositBillButton = new JButton();
            depositBillButton.setText("Deposit Bill");
0
Comment
Question by:urobins
  • 3
  • 3
6 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
ID: 18872851
billComboBox.getSelectedIndex();

gives the index of the selected item [0..N] or -1 if a new item was typed in.

1 Dollar yields 0
5 Dollars yields 1
7 Dollar will produce -1 because it was obviously typed.

billComboBox.getSelectedItem();

gives the selected item at once without indicating new or old.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18872854
And don't forget:

billComboBox.addActionlistener(this); // Makes life easier.

;JOOP!
0
 

Author Comment

by:urobins
ID: 18873000
Thanks I'll try this out and post back!
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:urobins
ID: 18873199
that worked perfect thanks!

although I didn't have to put the actionlistener and when I tried it gave me an error???

Thanks again
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18873204
Of course an error when your class does not

"implements ActionListener"

forcing a method to be added:

"public void actionPerformed(ActionEvent e)"

But you seem to do without.

Happy Easter.
0
 

Author Comment

by:urobins
ID: 18873208
oh hahahah.... thanks!  I didn't realize that.

thanks have a happy Easter as well!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question