File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP 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: 19847

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...
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: JComboBox Action Issues.