File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getSelectedItem() selects nothing

 
alejandro garcia
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess showMessageDialog should display a string message rather than an object instance.
???
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
alejandro garcia
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't work
Now I run this:
javax.swing.JOptionPane.showMessageDialog(null,"Hello");
and got the same error
 
alejandro garcia
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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

 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic