• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Obtaining the value of a row from a JComboBox

 
matthew weeks
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a Swing question. Moving...
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
matthew weeks
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
value.setText((String)box.getSelectedItem());
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic