File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with JComboBox background colour when using customer renderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with JComboBox background colour when using customer renderer" Watch "Problem with JComboBox background colour when using customer renderer" New topic
Author

Problem with JComboBox background colour when using customer renderer

Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
Hi all,

I've got a small problem in my GUI, which I cant seem to fix. Below is a screenshot of my GUI and I wanted all the box background colours to be pale pink, and all the ComboBoxes to look like the Pattern Supplied combo box (Pink unselected and white list items. The problem I have is that the Customer JComboBox is bound to a list database objects, so I'm using a custom renderer to display the name of the Customer. However this is preventing the JComboBox.setBackground() method from working.

I've tried editing the list background in the renderer, - this makes the unselected combobox pink, but also makes the list items pink too. I've also tried overriding paint, and painting the background pink - this also works. However it paints the list pink and prevents list.setBackground() from working, preventing me from making the list background white.



This issue is driving me insane and I'd be extremely greatful if someone who understands this better could help.

Many thanks in advance
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
If Customer combobox is not an editable field (that is, user can't type any custom values in it) , the ListCellRenderer is used to render the list items as well as the selected item.
It can detect whether it's rendering the selected item or a list item using index. If index=-1, it's rendering the selected item and can set background color accordingly. Javadoc of setRenderer:

Sets the renderer that paints the list items and the item selected from the list in the JComboBox field. The renderer is used if the JComboBox is not editable. If it is editable, the editor is used to render and edit the selected item.
The default renderer displays a string or an icon. Other renderers can handle graphic images and composite items.

To display the selected item, aRenderer.getListCellRendererComponent is called, passing the list object and an index of -1.


If it is an editable field, then the selected item should be rendered by a ComboBoxEditor implementation, not by the ListCellRenderer component.
Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
Thanks for the reply.

In that case, how would I set the colour of the selected index? Here is the code for my renderer as it stands:



Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Call setBackground(...) with a value dependent on the isSelected argument.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
Rob Spoor wrote:Call setBackground(...) with a value dependent on the isSelected argument.


Hi,

I tried that but it doesn't give the behaviour I required. Here is my code:



What happens is the combobox appears white on the GUI (i require it to be pink) when it's out of focus. I have no issues with the background of the list, but I am unable to set the JComboBox default colour when its not being used.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
The arguments received in getListCellRendererComponent() are used to decide rendering context behaviour. Based on the context, set the *renderer component* properties, not the list's.
As I understood your requirements, you have a noneditable combobox which should always have pink background, and its dropdown list should always have white background , is this correct?
For that
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
This doesn't seem to be all that simple. It behaves differently in different look and feels - in Metal, the background color is simply ignored for the body but used for the arrow(??) and the list, while in Windows system look and feel, it works fine.
What L&F are you using - what's the OS?
In my Win7 system, I had to just setBackground of combobox to pink; it worked as expected even with a custom renderer and regardless of focus. I didn't need to set background of renderer at all.
But it worked very differently in other L&Fs.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> so I'm using a custom renderer to display the name of the Customer

instead of using a renderer, create a Customer class where you override
toString() to return the customer's name, then just read the db data into
Customer objects and add them to the comboBox (which will display only
the customer's name).
Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
Karthik Shiraly wrote:This doesn't seem to be all that simple. It behaves differently in different look and feels - in Metal, the background color is simply ignored for the body but used for the arrow(??) and the list, while in Windows system look and feel, it works fine.
What L&F are you using - what's the OS?
In my Win7 system, I had to just setBackground of combobox to pink; it worked as expected even with a custom renderer and regardless of focus. I didn't need to set background of renderer at all.
But it worked very differently in other L&Fs.




Hi,

I'm using a customer L&F from http://www.jtattoo.net/ which goes by the name of Acryl w/ Win 7. I looked at the source code of the L&F to see how it worked but it was a little too complex to follow for my small brain.

I tried your snippet of code but it still rendered white when not in focus, and the list background was pink (not white).

Thanks for the help though.
Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
Michael Dunn wrote:> so I'm using a custom renderer to display the name of the Customer

instead of using a renderer, create a Customer class where you override
toString() to return the customer's name, then just read the db data into
Customer objects and add them to the comboBox (which will display only
the customer's name).


Hi Michael,

Thanks for the reply, I've tried your suggestion and it worked. Great thinking outside the box, cheers.

Regards,

Ste
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with JComboBox background colour when using customer renderer
 
Similar Threads
Disable items on JComboBox
Cell renderer for different JComboBox's in same column?
Jtree Ugly Look
JTable +JComboBox
JComboBox Vertical Scrollbar not showing up.