| 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: 19216
|
|
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
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
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
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
Joined: Sep 20, 2005
Posts: 852
|
|
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: 19216
|
|
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: 8434
|
|
shraddha sawant, Your post was moved to a new topic.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
 |
|
|
subject: JComboBox.setModel (with enums)
|
|
|