aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes getSelectedItem() selects nothing   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getSelectedItem() selects nothing   " Watch "getSelectedItem() selects nothing   " New topic
Author

getSelectedItem() selects nothing

alejandro garcia
Greenhorn

Joined: Aug 11, 2007
Posts: 10
Hi! I'm trying to show the item selected in a combo box with getSelectedItem(), when the user clicks the button I just try to show the item with showMessageDialog, but it selects nothing and I can't find out why, could anybody help please?


Hui Zhao
Ranch Hand

Joined: Jul 09, 2007
Posts: 116
I guess showMessageDialog should display a string message rather than an object instance.
???
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959


I guess showMessageDialog should display a string message rather than an object instance.

No, that's not the problem. The problem is because you never initialize the langList.
Inside initComponents()

You should remove the JComboBox since you already have the same variable name for the instance variable.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
alejandro garcia
Greenhorn

Joined: Aug 11, 2007
Posts: 10
didn't work
Now I run this:
javax.swing.JOptionPane.showMessageDialog(null,"Hello");
and got the same error
alejandro garcia
Greenhorn

Joined: Aug 11, 2007
Posts: 10
I don't understand: I can't remove this line
JComboBox langList = new JComboBox(langStrings);
from the initComponents()

and I can't remove this attribute from the class:
private javax.swing.JComboBox langList;


Hui Zhao
Ranch Hand

Joined: Jul 09, 2007
Posts: 116
try this
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959


I can't remove this line
JComboBox langList = new JComboBox(langStrings);

Is it because the IDE, i.e. NetBeans doesn't allow you to do that?

Actually what I was trying to say was that you never initialized the langList instance variable. To illustrate your problem, this might give you and idea why your code doesn't work.

If you use NetBeans, when you drag a component into the JFrame, it normally creates an instance variable of that component and then in the initComponent() method, it will do the initialization.

Hope this helps.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I don't understand: I can't remove this line
> JComboBox langList = new JComboBox(langStrings);
> from the initComponents()

> and I can't remove this attribute from the class:
> private javax.swing.JComboBox langList;

you have duplicate declarations for 'langList'

by including the 'type' JComboBox you make langList local to initComponents()
the other langList is not initialised, and is the one referenced from the button

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getSelectedItem() selects nothing
 
Similar Threads
need help in this
change string labels of a whole application
almost working: change string labels of whole application
Passing Variable Beetween GUI's
Internationalization: Select language from combo box