Re: disabling JComboBox options

From: VisionSet (spam_at_ntlworld.com)
Date: 06/30/04


Date: Wed, 30 Jun 2004 12:12:01 +0100


"hilz" <hilz@noway.com> wrote in message
news:gcrEc.16524$x9.13609@nwrddc02.gnilink.net...
> Mike:
> thank you for the idea.
> it did not really work very well, maybe i am missing something.
> the setEnabled(false) makes the list cell disabled (the color of the text
> is greyed), but it still can be selected.
> what do i need to prevent it from being selectable?
> is there something i am missing here?
>
> here is the code of the list cell rendere i implemented:
>
> class RegionListCellRenderer extends JLabel implements ListCellRenderer {
> public RegionListCellRenderer() {
> setOpaque(true);
> }
> public Component getListCellRendererComponent(
> JList list,
> Object value,
> int index,
> boolean isSelected,
> boolean cellHasFocus){
> boolean isCellSelectable=false;
>
> if(some condition here){
> isCellSelectable=false;
> }
> else{
> isCellSelectable=true;
> }
>
> setText(value.toString());
>
> this.setEnabled(isCellSelectable);
>
> if (isSelected && isCellSelectable) {
> setBackground(list.getSelectionBackground());
> setForeground(list.getSelectionForeground());
> } else {
> setBackground(list.getBackground());
> setForeground(list.getForeground());
> }
> return this;
> }
> }
>

If you do something like this as well:

 static class MyComboBox extends JComboBox {

  public MyComboBox(Object[] objs) {
   super(objs);
  }

  public void setSelectedIndex(int i) {
     if(i != 2) super.setSelectedIndex(i);
  }
 }

Should achieve the desired affect.
There is probably a better way.

--
Mike W


Relevant Pages

  • Re: major DNS hiccup
    ... Mike, can you have a look at your named logs, and see when it started? ... I see reply packets with correct checksums and no noticeable missing packets - so they'd have to be intercepting DNS packets, garbaging and retransmitting them: somehow I doubt ntl could manage that quite so successfully :-) ... Please use the corrected version of the address below for replies. ...
    (comp.unix.bsd.freebsd.misc)
  • RE: Decrementing row after deletion
    ... Thanks Mike - you are a star, but that was a bit of an easy one! ... I''''m not a complete idiot - there are some parts missing! ... If MyPos> 0 Then ...
    (microsoft.public.excel.programming)
  • Re: PARTING OUT THE FOLLOWING GAMES: Elvira and the Party Monsters, White Water, Road Show, Star Tre
    ... Mike, email sent. ... RS is missing jaw assemblies and most head parts, ...
    (rec.games.pinball)
  • Re: Outlook 2002, warnings
    ... > What am I missing? ... >>> Mike, I've looked at the articles but I am wallowing in the dark here. ... >>> Ian ...
    (microsoft.public.fox.helpwanted)

Loading