• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

getSelectedItem() selects nothing

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess showMessageDialog should display a string message rather than an object instance.
???
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic