Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing Background Colour of Selected Item in JComboBox  RSS feed

 
Malcolm Clews
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have an application developed with jdk1.4.2, runs on jre1.4.2.

This is now being migrated to run on jre6, built with jdk1.6, making minimal code changes to achieve the migration to supported versions of java, JBoss etc.

A search screen is created involving class SearchPanel which extends JPanel.

A number of components are created and initialised, including a call to method 'makeMandatory':-

makeMandatory(startDateTextField);
makeMandatory(endDateTextField);
makeMandatory((JTextField) reasonForSearchComboBox.getEditor().getEditorComponent());
makeMandatory(orderResultsByComboBox);
makeMandatory(residentFromComboBox);
makeMandatory(residentToComboBox);
makeMandatory(personRoleComboBox);


Code for makeMandatory method:-

/**
* Give a component the mandatory background and foreground colours.
* @param component The component to make mandatory./
*/
private void makeMandatory(JComponent component)
{
component.setBackground(ColourTypes.MANDATORY_TEXTFIELDS_BACKGROUND_COLOUR);
component.setForeground(ColourTypes.MANDATORY_TEXTFIELDS_FOREGROUND_COLOUR);
}


This all works fine for the original app compiled and running on 1.4.2, i.e. the background colour is being changed on all these components.

On the migrated app, the background colour changes on all these components except for the JComboBox reasonForSearchComboBox.

I'm not a Swing expert by any stretch of the imagination, anyone have any ideas re this? Have scanned this forum quickly, will look more in depth whilst awaiting a reply.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make it work with a Renderer
http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer

As you can see the getListCellRendererComponent() method accepts a parameter isSelected, based on which you can change the background.

In a nutshell
1) extend JLabel and implement ListCellRenderer
2) In the getListCellRendererComponent method, check if isSelected is true/false and based on it, change the label background.
3) Dont forget to set Label#setOpaque(true), as by default, it is false (transparent)
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JComboBoxes and JLists use ListCellRenders; check those out.
 
Malcolm Clews
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh.

I've had a quick look at that link - this JComboBox is editable, will this cause a problem with this solution? (Apologies if the answer to this question is blindingly obvious once I have read through the tutorial in depth).

Haven't made any (client) code changes, to help my understanding, any idea why (the original code) worked on 1.4.2 but not 1.6?

Regards,

Malcolm
 
Malcolm Clews
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob I'll look at that too.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JComboBox.getEditor() and ComboBoxEditor.getEditorComponent()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!