• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox.setModel (with enums)

 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to load a combobox with enums (shown below).
I'm using the form editor in Netbeans 6.1 where I've entered the following. This complies fine but doesn't do anything! Could someone show me what I'm doing wrong please?
NB: the method in RoomType.toString just uses a utility prog I wrote to convert the enums e.g (SINGLE_STANDARD to Single Standard).
Thanks in advance.



 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just tested it and it works fine with me (well, after removing the Utilities part of the toString):

And just a small correction, it's linen room, not linin room.
[ November 10, 2008: Message edited by: Rob Prime ]
 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for checking the code Rob (and for pointing out that awful spelling error ). I've not really used NetBeans form editor before. Would it be ok to post this question in the IDE forum? I'm just wondering if the automatic code generation has an issue with this method?

Many Thanks,

David
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Stevens:
Thanks for checking the code Rob (and for pointing out that awful spelling error ). I've not really used NetBeans form editor before. Would it be ok to post this question in the IDE forum? I'm just wondering if the automatic code generation has an issue with this method?

Many Thanks,

David


Are you saying it isn't showing up in the design view or it doesn't work correctly when you run the program? It probably won't work in design view, FWIW.
 
Brian Cole
Author
Ranch Hand
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Stevens:
Hi, I'm trying to load a combobox with enums (shown below).






Is there a reason you're converting the enum values to strings before populating the combo box model?

Wouldn't it be easier to just do

comboRoomType.setModel(new DefaultComboBoxModel( RoomType.values() ));

This way comboRoomType.getSelectedItem() is the actual enum value instead of a string that might need to be converted, which could be contributing to your "doesn't do anything" problem.
 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many apologies everyone I am suitably embarressed . For some stupid reason I was expecting the code to work in preview mode! The code does indeed work fine when I run the code. And yes, RoomType.values() works also. (I had tried that previously and thought it didn't work, since I was just looking at the preview).

It's times like this when I wish you guys weren't so quick at being helpful. Maybe I would have realised my idiotic mistake and saved myself this embarresment

Oh well, you live and learn. Thanks for the help and sorry it was such a silly error on my part.

David
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:
Is there a reason you're converting the enum values to strings before populating the combo box model?

Wouldn't it be easier to just do

comboRoomType.setModel(new DefaultComboBoxModel( RoomType.values() ));

This way comboRoomType.getSelectedItem() is the actual enum value instead of a string that might need to be converted, which could be contributing to your "doesn't do anything" problem.

And you can use a CellRenderer to display your enums more friendly.
 
Maneesh Godbole
Saloon Keeper
Posts: 11060
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shraddha sawant,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic