• 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

Using a custom renderer for JComboBox

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this for the combo backgrounds



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

 
Biliang Zhou
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".

 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic