Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

disable jcombobox item

1,220 views
Skip to first unread message

johnmmcparland

unread,
Apr 27, 2011, 11:44:24 AM4/27/11
to
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

RedGrittyBrick

unread,
Apr 7, 2008, 5:20:10 AM4/7/08
to
johnmmcparland wrote:
> 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.
>

The way I deal with invalid items is to just show the user the currently
valid items. I don't leave the invalid items visible and selectable.

--
RGB

johnmmcparland

unread,
Apr 7, 2008, 5:32:41 AM4/7/08
to

>
> The way I deal with invalid items is to just show the user the currently
> valid items. I don't leave the invalid items visible and selectable.
>
> --
> RGB

Yes I'm aware that's often the case but not this time.

Another way to reprahse this question would be "how do I render the
selected item in a JComboBox differently (when it is collapsed" -
maybe it helps to think of it that way.

David A. Redick

unread,
Apr 9, 2008, 5:01:45 PM4/9/08
to
On Apr 7, 5:32 am, johnmmcparland <johnmmcparl...@googlemail.com>
wrote:

The only way I've been able to do it is with monkeying with the
ComboBoxEditor.

add this code to your sample.

[code]
comboBox.setEditor(new MyComboBoxEditor());
comboBox.setEditable(true);

....

private class MyComboBoxEditor extends BasicComboBoxEditor
{
public Component getEditorComponent()
{
Component p = super.getEditorComponent();

p.setBackground(Color.YELLOW);
return p;
}
}
[/code]

The problem is now is how to allow the editor to be enabled /AND/ not
allow the user to edit the item.
Not sure about that one....

David A. Redick

unread,
Apr 9, 2008, 5:03:08 PM4/9/08
to
oh and

import javax.swing.plaf.basic.BasicComboBoxEditor;

David A. Redick

unread,
Apr 9, 2008, 5:20:31 PM4/9/08
to
Okie dokie... I think I've figured it out.

Issues with this solution.
1) I'm getting the default background during the constructor. Could
cause fubars.
2) I'm relying on the BasicComboBoxEditor.editor object to be a
protected JTextField (in SE6 it is).
3) To get the dodgy object we have to make the comboBox an instance
object (or member object or wtf ever).

Seems to work rather well... nasty tho' and will prob. break on ever
other version of java.

[code]


private class MyComboBoxEditor extends BasicComboBoxEditor
{

Color origBG;

MyComboBoxEditor()
{
super();

editor.setEditable(false);
origBG = editor.getBackground();
}

public void setItem(Object anObject)
{
super.setItem(anObject);

if(comboBox.getSelectedIndex() == dodgySelection)
{
editor.setBackground(Color.YELLOW);
}
else
{
editor.setBackground(origBG);
}
}
}
[/code]

0 new messages