Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

disable item in JComboBox

843806Apr 7 2008 — edited Apr 7 2008
Hi all,

I would like to "disable" and item in a JComboBox. By "disable" I mean grey-out so as not to prohibit selection of that item by a user but show them that this option shouldn't be selected.

I can get my app to "grey out" the item in the list by creating my own renderer however when that item has been selected the background colour is still white - giving no indication to the user that this is a "bad" selection. Is there some way I can "grey out" this item when it is selected.

The reason for "greying out" rather than removing is because the user has switched modes from one where the item was valid to one where it isn't.

Example;
package ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class ComboBoxToy extends JFrame
{
   public static final long serialVersionUID = 0;

   protected JTextArea      textArea;
   
   protected int dodgySelection = 1;

   public ComboBoxToy()
   {
      // Setup JFrame stuff
      setTitle("Combo Box Rendering");
      setSize(150, 100);      
      setResizable(false);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Setup the combo box
      JComboBox comboBox = new JComboBox();
      comboBox.addItem("Bob");
      comboBox.addItem("Dave");
      comboBox.addItem("Susan");
      comboBox.addActionListener(new ComboBoxActionListener());
      comboBox.setRenderer(new ComboBoxListRenderer());

      // Setup the text area
      textArea = new JTextArea(1, 20);

      // Add the components to the content pane
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(comboBox,BorderLayout.NORTH);
      getContentPane().add(textArea,BorderLayout.CENTER);

      // Show it!
      setVisible(true);
   }

   public static void main(String args[])
   {
      try
      {
         // Set System L&F
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch (UnsupportedLookAndFeelException e)
      {
         // handle exception
      }
      catch (ClassNotFoundException e)
      {
         // handle exception
      }
      catch (InstantiationException e)
      {
         // handle exception
      }
      catch (IllegalAccessException e)
      {
         // handle exception
      }
      // Create the GUI in a different thread
      SwingUtilities.invokeLater(new Runnable()
      {
         public void run()
         {
            new ComboBoxToy();
         }
      });
   }

   private class ComboBoxActionListener implements ActionListener
   {
      public void actionPerformed(ActionEvent ae)
      {
         textArea.setText("You selected "+(String) ((JComboBox) ae.getSource())
               .getSelectedItem());
      }
   }
   
   private class ComboBoxListRenderer extends BasicComboBoxRenderer
   {
      public static final long serialVersionUID = 0;
      
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
      {
         super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
         
         if (index == dodgySelection) 
         {
            setBackground(list.getParent().getBackground());
         }

         return this;
      }
   }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 5 2008
Added on Apr 7 2008
3 comments
2,302 views