• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

JComBox  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 21557
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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 ]
 
And inside of my fortune cookie was this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!