aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Obtaining the value of a row from a JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Obtaining the value of a row from a JComboBox" Watch "Obtaining the value of a row from a JComboBox" New topic
Author

Obtaining the value of a row from a JComboBox

matthew weeks
Greenhorn

Joined: Feb 23, 2004
Posts: 15
When you select a row within a JComboBox how do you get that row to be printed in a textfield. Am I right in thinking that you use the ItemSelected method?
Any help with this would be greatly appreciated.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This is a Swing question. Moving...


GenRocket - Experts at Building Test Data
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
you need to add a listener to the JComboBox. Either an ItemListener or ActionListner depending on your needs. Experiment with both.
When an event is fired call getSelectedIndex() on your combo box to get the selected row.
D.
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
If you want to write to a text field you could that wit

where tf is the name of your textfield and cb is the name of your JComboBox.
Ed


The nice thing about Standards is that there are so many to choose from!
matthew weeks
Greenhorn

Joined: Feb 23, 2004
Posts: 15
Just tried out what you said, and I get an error when compiling the code:-
setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
value.setText(box.getSelectedItem());
where value = textfield and box = jcombobox
This wouldn't be that the values in the combo box are obtained from a vector. If so how do I overcome this problem
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
value.setText((String)box.getSelectedItem());
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Obtaining the value of a row from a JComboBox