narasimha
narasimha

Reputation: 11

how to identify JComboBox whether is disabled or enable inside its renderer class

I have a JComboBox contains following iems

{ "select" , "one" , "two" }

i need a separate background for first item so , i have made a condition in it's renderer like

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
 boolean cellHasFocus) {

            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null){
                Item item = (Item)value;
                setText(item.getDescription());             
            }
            if(index==-1){
                setBackground(new Color(199,209,210));
                setForeground(Color.BLACK);
            }

            return this;
        }
}

so my question is if we disable a JComboBox, i have to made -1th index of component background color to someother color

like

if(index==-1){

   setBackground(Color.RED);
}

please advice

Upvotes: 1

Views: 305

Answers (2)

Avrom
Avrom

Reputation: 5027

Maybe I'm missing something here, but if a JComboBox is disabled, then that means that it cannot popup its list of items. But if it is not showing its list of items, then that means it is not using the renderer for that list. So, why do you need to get a reference to the combobox in the renderer?

Upvotes: 0

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17369

The simplest way is always the best. Since you assigning renderer to a combobox, why don't you pass the combobox into it? Just create a custom renderer that holds the reference to a combobox then use the stored reference inside of your getListCellRendererComponent method

Upvotes: 2

Related Questions