• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComBox  RSS feed

 
Bernardo Costa
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 ]
 
Rob Spoor
Sheriff
Posts: 20892
81
Chrome Eclipse IDE Java 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.
 
Jelle Klap
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!