Set Background/Foreground color for Cell Renderer in JComboBox

Hello,

I was wondering if there is a way to change default settings for when I browse items under a JComboBox's cell renderer? I want the item's color to change (to what I set it to), when mouse enters the item. As of now, the cell's background color changes to Blue when I enter it. Is this default for JComboBox or might it have been set somewhere, that I need to look into?

Please let me know if there are ways to do this.

Thanks!

Message was edited by:

programmer_girl

[513 byte] By [programmer_girla] at [2007-11-27 8:21:33]
# 1

Here's my SSCCE:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JComboBox;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.text.DefaultHighlighter;

public class MyComboBoxTest extends JPanel {

/**

* @param args

*/

String[] patterns = {"pattern1","pattern2","pattern3"};

public MyComboBoxTest()

{

JPanel patternsPanel = new JPanel();

setMinimumSize(new Dimension(100, 100));

JComboBox patternList = new JComboBox(patterns);

final JTextField editor = (JTextField) patternList.getEditor().getEditorComponent();

patternList.setEditable(true);

this.add(patternsPanel);

patternsPanel.add(patternList);

patternList.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

editor.setSelectedTextColor(Color.WHITE);

editor.setForeground(Color.WHITE);

try{

editor.getHighlighter().addHighlight(0, editor.getText().length(),new DefaultHighlighter.DefaultHighlightPainter(Color.BLUE));

} catch (Exception ex){

ex.getMessage();

}

}

});

editor.addMouseListener(new MouseAdapter()

{

public void mouseClicked(MouseEvent e)

{

editor.getHighlighter().removeAllHighlights();

editor.setForeground(Color.BLACK);

editor.setSelectedTextColor(Color.BLACK);

}

});

}

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame frame = new JFrame("My ComboBox Demo");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.setSize(100, 100);

frame.getContentPane().add(new MyComboBoxTest());

frame.setLocation(50, 50);

frame.pack();

frame.setVisible(true);

}

}

programmer_girla at 2007-7-12 20:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

Stay focussed sweetheart. You are tatlking renderer, yet code says Editor.

Check out http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html

for how to change the JComboBox renderer and how to change the colour of the ComboBox highlight when the item in the list has the mouse over it. (focus on the isSelected condition in the getListCellRendererComponent( ) method )

ICE

icewalker2ga at 2007-7-12 20:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks for your help. I actually looked at this before I posted this question. But this time, I took a harder look. In the getListCellRendererComponent method, I'm not sure at all what to use as JList, value or index. Please help me out.Thanks!
programmer_girla at 2007-7-12 20:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 4

For example if you want to set the selection background to red:patternList.setRenderer(new DefaultListCellRenderer() {

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (isSelected)

comp.setBackground(Color.RED);

return comp;

}

});

Rodney_McKaya at 2007-7-12 20:10:05 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thank you so much! It works :)
programmer_girla at 2007-7-12 20:10:05 > top of Java-index,Desktop,Core GUI APIs...