aspose file tools*
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
Author

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.addItem("Planning");
comboBox.addItem("Testing");
comboBox.setBackground(new Color(153, 204, 255));
ComboBoxRenderer renderer = new ComboBoxRenderer();
comboBox.setPreferredSize(new Dimension(80, 25));
comboBox.setRenderer(renderer);
return comboBox;
}

private static void createAndShowGUI()
{
JFrame frame = new JFrame("TestCustomRenderer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new TestCustomRenderer();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.pack();
frame.setVisible(true);
}

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

class ComboBoxRenderer extends BasicComboBoxRenderer
{
public ComboBoxRenderer() {
super();
setOpaque(true);
}

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setText(value.toString());
if(cellHasFocus)
{
setText("value");
}
else
{
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,Color.blue,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".

 
 
subject: Using a custom renderer for JComboBox
 
Similar Threads
Using JCheckBox as CellRenderer in JList
JComboBox
Right Align numbers in Jtable
How to set ID and Value for a JCombobox
Display value and Actual value in JComboBox