aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JComboBox.setModel  (with enums) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox.setModel  (with enums)" Watch "JComboBox.setModel  (with enums)" New topic
Author

JComboBox.setModel (with enums)

David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.


GenRocket - A Test Data Generation Platform
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Jul 26, 2007
Posts: 9990
    
    7

shraddha sawant,
Your post was moved to a new topic.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JComboBox.setModel (with enums)
 
Similar Threads
Adding JLabels to a vector
Sorting Of Array From Even Number To Odd Number
help with a booking system
ArrayList
Quick Question