Solved

how to determine which option in a combo box is selected

Posted on 2007-04-08
6
221 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 85
how do i compare an object based on two fields 6 48
nextBoolean(double p) for Random class 3 35
factorial example 4 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now