This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JComboBox Action Issues. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox Action Issues." Watch "JComboBox Action Issues." New topic

JComboBox Action Issues.

Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Hey guys, I'm having trouble using an actionListener with this JComboBox. Whenever I try to receive the selected index, it always returns "0" no matter what is selected inside the JComboBox.

Creation of the JComboBox:


Rob Spoor

Joined: Oct 27, 2005
Posts: 20048

You shouldn't create a new Gui instance but use the existing one. Because you create a new Gui instance you also create a new JComboBox for it, and it's this new JComboBox that you're getting the selected index of.

Maybe even better, use event.getSource():

How To Ask Questions How To Answer Questions
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Wow, that worked. I have no idea how though? Can you explain what event.getSource() does? In my eyes it just looks like your creating an instance of JComboBox and casting JComboBox objects on it...and then I get lost...
I agree. Here's the link:
subject: JComboBox Action Issues.
It's not a secret anymore!