Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java GUI > disable jcombob...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 9625 of 9821
Post > Topic >>

disable jcombobox item

by johnmmcparland <johnmmcparland@[EMAIL PROTECTED] > Apr 7, 2008 at 01:50 AM

Hi all,

I would like to disable an item in a JComboBox.  By "disable" I mean
to grey-it out or otherwise indicate to the user that this item is not
a valid selection.  They should still be able to select it but it
should look different.

I can get it to grey out the option when it is not selected but as
soon as I select it the item has a white background - i.e. looks no
different to the rest.

The reason for keeping the item and "greying out" is because the user
has switched modes from one where the item was valid to one where it
is not.

Code;

[code]
package ui;

im****t java.awt.BorderLayout;
im****t java.awt.Component;
im****t java.awt.event.ActionEvent;
im****t java.awt.event.ActionListener;

im****t javax.swing.JComboBox;
im****t javax.swing.JFrame;
im****t javax.swing.JList;
im****t javax.swing.JTextArea;
im****t javax.swing.SwingUtilities;
im****t javax.swing.UIManager;
im****t javax.swing.Unsup****tedLookAndFeelException;
im****t javax.swing.plaf.basic.BasicComboBoxRenderer;

public class ComboBoxToy extends JFrame
{

   public static final long serialVersionUID = 0;

   protected JTextArea      textArea;

   /**
    * The number of the item to render differently
    */
   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 (Unsup****tedLookAndFeelException e)
      {
         // handle exception
      }
      catch (ClassNotFoundException e)
      {
         // handle exception
      }
      catch (InstantiationException e)
      {
         // handle exception
      }
      catch (IllegalAcces***ception 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;
      }
   }
}

[/code]
 




 6 Posts in Topic:
disable jcombobox item
johnmmcparland <johnmm  2008-04-07 01:50:13 
Re: disable jcombobox item
RedGrittyBrick <RedGri  2008-04-07 10:20:10 
Re: disable jcombobox item
johnmmcparland <johnmm  2008-04-07 02:32:41 
Re: disable jcombobox item
"David A. Redick&quo  2008-04-09 14:01:45 
Re: disable jcombobox item
"David A. Redick&quo  2008-04-09 14:03:08 
Re: disable jcombobox item
"David A. Redick&quo  2008-04-09 14:20:31 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 4:44:02 CDT 2008.