disable jcombobox item
To: comp.lang.java.gui
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;
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;
/**
* 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 (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;
}
}
}
[/code]
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24