• 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:

JComBox

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think is a very simple question, I ve got a JComBox with an array of Strings. Something like this:

JPanel myPanel;
JComBox box;

String levels[]={"Level 1","Level 2","Level 3","Level 4","Level 5"};

box = new JComboBox (levels);
myPanel.add(box);

Ok. My question is: How can I let just the level 1 enable at first, or how can I enable or disable just one level of the JCombox, like level 1 and 2 enable but level 3, 4 and 5 disable.

Thanks a lot!! Hope someone helps me!!
[ March 26, 2008: Message edited by: Bernardo Costa ]
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JComboBox has some methods you can use:
- addItem(Object) adds an item to the end
- insertItemAt(Object, int) adds an item anywhere you want
- removeAllItems() removes everything
- removeItem(Object) removes the first item that equals() the given object
- removeItemAt(int) removes an indexed item

Please note that

This method works only if the JComboBox uses a mutable data model.


However, they way you created the JComboBox that is the case, so no worries.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JComboBox does not provide any means of disabling/graying-out one or more of its items. You could instead simply remove items that may not be selected, but if you desperately need to have the JComboBox display unselectable options you would need to create a custom implementation of ListCellRenderer for the gray-out visuals and intercept and ignore selection events for disabled items in the ActionListener.
 
Bernardo Costa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rob and Jelle... in the way Jelly said I found some code in this link:

http://www.codeguru.com/java/articles/165.shtml

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the basic idea, but I'm not thrilled about how this implementation ties logic that is of interest only to the view, directly to the model i.e the CanEnable interface (not a great name) for items.
[ March 27, 2008: Message edited by: Jelle Klap ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic