Enable/Disable JComboBox Items in Java Swing

Articles —> Enable/Disable JComboBox Items in Java Swing

A JComboBox is a java swing user interface component that provides a drop down menu allowing users to select a particular item. While the JComboBox is flexible and userful in many contexts, there may be times when one wishes to extends its behavior, for instance have a JComboBox behave in a similar way graphically to that of a JMenu - a behavior in which the programmer can enable and/or disable items based upon the state of the program.

From a user's perspective enabled states are often rendered differently than disabled states, thus one can provide a custom renderer for a JComboBox which renders items the appropriate color based upon its state - enabled or disabled. Reuse and abstraction is important, and to decouple the renderer discussed above from the JComboBox or its model, the class below relies on a ListSelectionModel that specifies items to be enabled or disabled. Although we could just as easily have this model define disabled items, for this article I will have this model specify which items are enabled.

Below is the java code for the class based upon the guidelines above:


import javax.swing.plaf.basic.BasicComboBoxRenderer;

import java.awt.Color;

import java.awt.Component;

import javax.swing.JList;

import javax.swing.UIManager;

/**

 * Class that can be used as a renderer for a JComboBox which enables/disables

 * items based upon a ListSelectionModel that specifies which items are enabled. 

 * @author Greg Cope

 *

 */

public class EnabledJComboBoxRenderer extends BasicComboBoxRenderer{

	

	static final long serialVersionUID = -984932432414L;

	

	private final ListSelectionModel enabledItems;

	

	private Color disabledColor = Color.lightGray;

	

	/**

	 * Constructs a new renderer for a JComboBox which enables/disables items

	 * based upon the parameter model.

	 * @param enabled

	 */

	public EnabledJComboBoxRenderer(ListSelectionModel enabled){

		super();

		this.enabledItems = enabled;

	}

	

	/**

	 * Sets the color to render disabled items. 

	 * @param disabledColor

	 */

	public void setDisabledColor(Color disabledColor){

		this.disabledColor = disabledColor;

	}

	

	/**

	 * Custom implementation to color items as enabled or disabled. 

	 */

	@Override

	public Component getListCellRendererComponent(JList list,

            Object value,

            int index,

            boolean isSelected,

            boolean cellHasFocus) {

		Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

		if ( !enabledItems.isSelectedIndex(index) ){//not enabled

			if ( isSelected ){

				c.setBackground(UIManager.getColor("ComboBox.background"));

			}else{

				c.setBackground(super.getBackground());

			}

			c.setForeground(disabledColor);

		}else{

			c.setBackground(super.getBackground());

			c.setForeground(super.getForeground());

		}

		return c;

	}

}

The above class extends the default JComboBox renderer, and colors enabled/disabled items as necessary. Note that the selection color is defined by the UIManager - selection background must be dealt with for disabled items otherwise the items may still appear selected as the user mouses over.

The example below demonstrates the use of this class:


import javax.swing.*;



/**

 * Class to demonstrate the use of the EnabledJComboBoxRenderer class. 

 * @author G. Cope

 *

 */

public class EnabledDisabledComboBoxExample {



	public EnabledDisabledComboBoxExample(){

		JFrame frame = new JFrame();

		JPanel jp = new JPanel();

		JComboBox combo = new JComboBox();

		combo.addItem("Testing...");

		combo.addItem("1...");

		combo.addItem("2...");

		combo.addItem("3...");

		combo.addItem("4...");

		combo.addItem("Wait...");

		combo.addItem("Go...");

		DefaultListSelectionModel model = new DefaultListSelectionModel();

		model.addSelectionInterval(0, 3);

		model.addSelectionInterval(6, 6);

		EnabledJComboBoxRenderer enableRenderer = new EnabledJComboBoxRenderer(model);

		combo.setRenderer(enableRenderer);

		jp.add(combo);

		frame.add(jp);

		frame.pack();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setVisible(true);

	}

	public static void main(String[] args) throws InterruptedException, InvocationTargetException{

		SwingUtilities.invokeAndWait(new Runnable(){

			@Override

			public void run() {

				new Test();

			}

			

		});

		

	}

}

Voila, a behavior of a JComboBox that allows the enabling and disabling of its items through the model interface.

JComboBox showing items enabled and disabled

The above example class showing a JComboBox with items disabled.

Some reference:




Comments

  • Java Java   -   August, 5, 2014

    WESOME

  • Yanic Inghelbrecht   -   August, 30, 2014

    Thanks for the write up.

    new Test() in the invokeLater call should be new EnabledDisabledComboBoxExample()

    Also, disabled items are still selectable.

Back to Articles


© 2008-2022 Greg Cope