• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with JComboBox background colour when using customer renderer

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call setBackground(...) with a value dependent on the isSelected argument.
 
Ste Hutton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic