This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox getSelectedItem() only returning first element  RSS feed

 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've read there have been a few issues with getSelectedItem() in the past, I just wanted to verify if this is the case or not.

I have a FindDialog class where a JComboBox nameCombo; static reference is declared. There is a lazy initialization with function setCombos(), this function is called by another class in referencing this JComboBox. After initialization of FindDialog() constructor the actual elements listed in the ComboBox are correct. However, once an item is selected and the findButton is then clicked, the getSelectedItem() method of the ComboBox only seems to return the first element of the ComboBox whatever the actual selection was. In fact, I have two separate combox boxes and the same problem occurs with both.

I've made other simpler programs and the issue doesn't occur. The simpler programs do not have a lazy initialization of the values however, that's something I've yet to try out to see if this is the cause.

Has anyone come across this issue with getSelectedItem() before?

Many Thanks,
Ehsan

 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone come across this issue with getSelectedItem() before?


No.

I've made other simpler programs and the issue doesn't occur.


And you just proved its your code, not the combo box.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks.

As for this one I've discovered what's causing the bug, it was due to the static JComboBox reference and the way it was being invoked.

This thread can be closed.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!