Raging Goblin

4 May 2010

JCombobox with disabled items

Filed under: Java — Raging Goblin @ 13:21
Tags: , ,

I saw a couple of posts (here and here) about a common problem in Java: creating a JCombobox with some items disabled. To give an idea to what I mean here is a screenshot. It shows a JCombobox with some items in gray that cannot be chosen:

DisabledComboboxItem

Here is my solution to this:

import java.awt.Component;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class DisabledItemsComboboxDemo extends JFrame {

 public static void main(String[] args) {
  DisabledItemsComboboxDemo frame
    = new DisabledItemsComboboxDemo("Combobox with disabled items");
  frame.setVisible(true);
 }

 public DisabledItemsComboboxDemo(String title) {
  super(title);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.init();
 }

 private void init() {
  this.setSize(500, 50);
  DisabledItemsComboBox box = new DisabledItemsComboBox();
  box.addItem("This item is disabled but selected at first appearance",
              true);
  box.addItem("This item is disabled and you cannot select it", true);
  box.addItem("This is the first selectable item");
  box.addItem("This is the second selectable item");
  box.addItem("This is the third selectable item");
  add(box);
 }

 private class DisabledItemsComboBox extends JComboBox {

  public DisabledItemsComboBox() {
   super();
   super.setRenderer(new DisabledItemsRenderer());
  }

  private Set disabled_items = new HashSet();

  public void addItem(Object anObject, boolean disabled) {
   super.addItem(anObject);
   if (disabled) {
    disabled_items.add(getItemCount() - 1);
   }
  }

  @Override
  public void removeAllItems() {
   super.removeAllItems();
   disabled_items = new HashSet();
  }

  @Override
  public void removeItemAt(final int anIndex) {
   super.removeItemAt(anIndex);
   disabled_items.remove(anIndex);
  }

  @Override
  public void removeItem(final Object anObject) {
   for (int i = 0; i < getItemCount(); i++) {
    if (getItemAt(i) == anObject) {
     disabled_items.remove(i);
    }
   }
   super.removeItem(anObject);
  }

  @Override
  public void setSelectedIndex(int index) {
   if (!disabled_items.contains(index)) {
    super.setSelectedIndex(index);
   }
  }

  private class DisabledItemsRenderer extends BasicComboBoxRenderer {

   @Override
   public Component getListCellRendererComponent(JList list,
                                                 Object value,
                                                 int index,
                                                 boolean isSelected,
                                                 boolean cellHasFocus) {

    if (isSelected) {
     setBackground(list.getSelectionBackground());
     setForeground(list.getSelectionForeground());
    } else {
     setBackground(list.getBackground());
     setForeground(list.getForeground());
    }
    if (disabled_items.contains(index)) {
     setBackground(list.getBackground());
     setForeground(UIManager.getColor("Label.disabledForeground"));
    }
    setFont(list.getFont());
    setText((value == null) ? "" : value.toString());
    return this;
   }
  }
 }
}
Advertisements

3 Comments »

  1. […] Posted May 17, 2010 Filed under: Java | Tags: Graphical User Interface, JComponent, Swing | In my last post I spoke about a JCombobox with the ability to add disabled items. Today I muse a little more on the […]

    Pingback by JCombobox with extra items that are not part of the list « Raging Goblin — 17 May 2010 @ 18:13 | Reply

  2. The disabled items can still be selected using the arrow keys.. any suggestions?

    Comment by Robert Fleming — 8 February 2012 @ 03:51 | Reply

  3. Using a arch linux system with jdk 7 you cannot select the items with the arrow keys. Try to register it as keylistener and alter the functionality of the key events: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

    Comment by raginggoblin — 27 February 2012 @ 11:27 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: