• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

how to determine which option in a combo box is selected

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
urobins
Asked:
urobins
  • 3
  • 3
1 Solution
 
sciuriwareCommented:
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
 
sciuriwareCommented:
And don't forget:

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

;JOOP!
0
 
urobinsAuthor Commented:
Thanks I'll try this out and post back!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
urobinsAuthor Commented:
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
 
sciuriwareCommented:
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
 
urobinsAuthor Commented:
oh hahahah.... thanks!  I didn't realize that.

thanks have a happy Easter as well!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now