Solved

how to determine which option in a combo box is selected

Posted on 2007-04-08
6
220 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
java set up 1 46
JAVA part two 5 41
Cipher Configuration on Apache HTTPD 4 22
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

21 Experts available now in Live!

Get 1:1 Help Now