my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes Using a custom renderer for JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using a custom renderer for JComboBox" Watch "Using a custom renderer for JComboBox" New topic

Using a custom renderer for JComboBox

Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
Nice Friday, guys (a little chilly though...).

I have successfully created a custom renderer for a JComboBox (to change the background color of the pull-down menu, so this color agrees with the background color of the JPanel that is holding the JComboBox, which has been changed to light blue).

But I still have two problems:

First, the combobox lost the mouseover effect - the default renderer works like if you bring up the pulldown menu, and mouseover any item, that item will be highlighted. With my custom renderer, this effect is gone...

Second, is it possible to create a gradient effect on the JComboBox? Like the up-to-down or left-to-right gradient. I wonder whether I need a different look and feel scheme to achieve this...

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class TestCustomRenderer extends JPanel{

JComboBox comboBox;

public TestCustomRenderer()
super(new BorderLayout());
this.setPreferredSize(new Dimension(200, 100));
this.add(get_comboBox(), BorderLayout.SOUTH);
this.setBackground(new Color(153, 204, 255));

private JComboBox get_comboBox()
comboBox = new JComboBox();
comboBox.setBackground(new Color(153, 204, 255));
ComboBoxRenderer renderer = new ComboBoxRenderer();
comboBox.setPreferredSize(new Dimension(80, 25));
return comboBox;

private static void createAndShowGUI()
JFrame frame = new JFrame("TestCustomRenderer");

JComponent newContentPane = new TestCustomRenderer();


public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

class ComboBoxRenderer extends BasicComboBoxRenderer
public ComboBoxRenderer() {

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setBackground(new Color(153, 204, 255));
return this;

[ November 03, 2006: Message edited by: Biliang Zhou ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try this for the combo backgrounds

here's a gradient button (not sure where this came from)

Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
Thank you, Michael!

Now I can use the recursive algorithm you showed me yesterday to traverse through all the comboboxes and give them different background colors.

And the GradientPaint example is just easy to follow - basically we can use the GradientPaint(x1,y1,,x2,y2,Color.white,true) to generate color patterns. It all depends the setting of the coordinates how the color pattern is going to "gradiate".

I agree. Here's the link:
subject: Using a custom renderer for JComboBox
It's not a secret anymore!