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

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);
}

Similar Messages

  • How do you set the font color for a specific entire row inside a JTable?

    How do you set the font color for a specific entire row inside a JTable?
    I want to change the font color for only a couple of rows inside a JTable.
    I've seen some ways to possibly do this with an individual cell.
    Clarification on changing the font color in an individual cell would be helpful too if
    there is no easy way to do this for a row.

    hai,
    Try out with this piece of code.Create your table and assign the renderer to each column in the table.
    CellColorRenderer m_CellColorRenderer = new CellColorRenderer();
    for(int i=0;i<your_JTable.getColumnCount();i++)
    your_JTable.getColumnModel().getColumn(i).setCellRenderer(m_CellColorRenderer);
    class CellColorRenderer extends JLabel implements TableCellRenderer
    CellColorRenderer()     
    setOpaque(true);     
    setHorizontalAlignment(LEFT);
    setVerticalAlignment(CENTER);
    setBackground(Color.white);
    setForeground(Color.black);
    protected void setValue(Object value)
         setText((value == null) ? "" : value.toString());
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row,int column)
         if(isSelected == true)
              setForeground(Color.red);
         else
              setForeground(Color.black);
         setValue(value);
         return this;
    regards,
    bala

  • How can i set the alternating colors for a table rows

    Dear All,
    Please any one help me how can i set the Alternating colors for Table Rows.
    i created a theam there i set the background alternating color to brown and i set the table design properity to alternating. but it is not reflecting.

    Hi,
    The design property in Table properties should work for your requirement. Select "alternating" value for design.
    Please see the API below:
    design
    Determines the appearance of the table. The property design can take the following values and is represented by enumeration type WDTableDesign.
    alternating - The table rows are displayed alternately in a different color.
    standard - The table background has one color. The individual table rows are displayed with grid net lines.
    transparent - The table background is transparent. The individual table rows are displayed without grid net lines.
    Check whether you have changed the right property or not? Also table should contain more than one rows to test this scenario.
    Regards,
    Jaya.
    Edited by: VJR on Jun 17, 2009 6:43 PM

  • Setting a default color for messages in Mail

    I figured out how to change the default font & font size in messages in the Mail application, and how to change the color in my signature, however I cannot figure out how to set the default color for new messages?

    Hi,
    Look at the Mail's preferences, under Composing. Select where you like to save notes and todos.

  • How do i set a multiple colors for a panel

    hi all,
    how do i set a multiple colors for a panel .pl desperately need a solution.
    regards
    bhaskar

    Iam contradicting self !
    Hope this helps !
    java.lang.Object
    |
    --java.awt.Component
    has the paint method ! Take the Graphics Object and call Graphics.setColor( Color.red ) .. and u can draw regular shapes like fillRoundRect. blah blah.. u need t count on the pixels .. I guess..
    An easier choice could be adding labels or panels with a layout of ur choice ..
    venu

  • Setting color for cells in a particular column of a jtable

    Hi, I know how to set the color for a cell in a column if the cell contains an Integer object and if I have default Integers already in the JTable when I begin. However I want the JTable to look blank at the beginning. As I can't make an Integer object invisible on the JTable I need to specify the colum in which I want the cells to change color in. I want to specify the column some how and then when the table later has Integers in it i can do a comparison to decide whether i want to change the color or not. Does anyone know how I will do this.
    Please HELP.

    1) You need to set the tooptip text on the renderer for the column (yourTable.getColumnModel().getColumn(...).getHeaderRenderer()).
    2) The header is a separate component to the table. When you add a table to a scroll pane it automatically adds the header to the scroll pane too. You can get the component (yourTable.getTableHeader()) and add it to your own container if you wish.

  • Cant change color on visited sites without changing background /foreground colors that hide options on the sitec

    I want to change the color of visited sites, but am forced to set background and foreground colors which don't properly
    show all of the colors for controls etc on many websites.
    I have to uncheck the button to Allow pages to choose their own colors....
    Google is a good example after doing a search, then change visited sites color, uncheck the allow pages button and then you can't see the search button and most of the options in the top right corner in google.
    I am a little color blind, so can't differentiate blue and purple for links on visited sites...
    thx

    I think the problem is that many sites use background colors to create buttons, so you lose that when you override the page's colors.
    Maybe you could use an add-on to fine-tune colors instead of Firefox's all-or-nothing setting. I found a couple extensions that say they can change page colors, but I haven't tried them myself:
    * [https://addons.mozilla.org/firefox/addon/color-that-site/ Color That Site!]
    * [https://addons.mozilla.org/firefox/addon/colorific-1/ Colorific]
    * [https://addons.mozilla.org/firefox/addon/toggledocumentcolors-198916/ ToggleDocumentColors_]
    * [https://addons.mozilla.org/firefox/addon/color-toggle/ Color toggle]
    For a truly custom approach to link colors, there are two general purpose ways to apply your own style rules to specific sites: the Stylish extension and a userContent.css files. You would need to set a preferred text ''and'' background color for visited and unvisited links so that you are guaranteed to be able to read the text.
    Hope one of these fits the bill!

  • Displaying different colors for cells/columns in Interactive Report

    Hi,
    I have a requirement to display just the 'cells' in the Interactive Report columns with different colors.
    There are 3 columns in the Interactive report.
    The color coding for all the 3 different columns should be such that:
    <40% - red color
    40-70-orange
    greater than 70%-green
    I tried the highlight option in the report and saved the report as default, but still the color condition ended up getting displayed to the end user. I dont want this.
    Can someone suggest a better way to handle this requirement?

    Perhaps explore similar solutions described here
    Background Colour of Report Cell Issues.
    Scott

  • Add ability to select background display color for transparent areas of image

    Right now the background color for images with an alpha channel if always white.  This makes Bridge useless for sorting through images where the primary color is white.  It also looks like crap when you are doing a slideshow preview on a black background.  It is only useful for people who do web page designs on a white webpage, but even then you can't tell if the image is cut out properly if it was cut from a white background.  The only way to check an image is to load it into Photoshop and create a background layer, which is pretty slow for previewing an entire directory of images.  The ability to set the alpha channel background color has been available for years in competing programs such as Thumbs+ so I am dumbfounded that it isn't yet in Bridge CS4.

    Agreed! Even primitive Mac Finder preview can show transparent alpha channel! Most Image browsing programs have this ability. Bridge CS4 is becomming finally usable, but this feature is a must!

  • How can i set the different colors for a different group ui elements

    Hi All,
                  I Created a View Container in that i crated 2 views. In first view i created a group and in second
    view also i created a group. I want to set The First view group header colour as Golden colour and for second view group heder colour as Green colour. Please any one help me on this.
    Thanks&Regards,
    Bhargava.

    Dear GLM,
                         i got that. while creating the theme, in Complex Elements select the group element and  set the primary group header colour to golden and secondary group Header colour to Green and in ur webdynpro application set the Group Design property as Primary Color for which group u want to set the Golden color and set the Group Design property as Secondary Color for which group u want to set the Green color.
    Thanks&Regards,
    Bhargava.

  • How to set canvas extension color for arbitrary image rotation?

    I'm using Photoshop CS4 v 11.0 with Windows XP.  When adjusting canvas size there's a menu for chosing the canvas extension color.  Where is the menu for chosing canvas extension color for arbitrary image rotation?
    Thanks.

    This will be the background colour selected in your toolbox.

  • How do I add "change background/foreground color" to the configurator, and color buttons?

    How do I add "change background color" and "change foreground color" in the configurator?
    Also is there a way to color buttons?
    Regards,
    Glen

    No matter whether you mean the application Configurator or a Panel created with Configurator you would probably do well to post the question on the Configurator Forum.
    Configurator

  • How to set disabled foreground color of JCheckBox?

    How do I set the disabled foreground color of JCheckBox? To clarify, I want to choose the color that is used to paint the component's foreground when it is disabled.
    thanks.

    Check out this thread:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=122112

  • Can I set a "default color" for new layers?

    I find myself constantly changing the color of new layers back to my favorite: Magenta. Is there a way to just make this the default new layer color?
    I understand the reasoning of using different colors for each layer, but so many standard colors do not preview well, with the artwork, I find them useless. I'd just as soon use one color.

    I have a hotkey for an action creating a new layer but unfortunately the layer color option cannot be recorded in the action. What I do is create a bunch of layers I would eventually need, select all of them in the Layers panel and double click on the selection to open the options dialog where I can change the layer color for all at once.

  • How to set cell background color for JCheckBox renderer in JTable?

    I need to display table one row with white color and another row with customized color.
    But Boolean column cannot set color background color.
    Here is my codes.
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.TreeSet;
    public class BooleanTable extends JFrame
        Object[][] data = {{Boolean.TRUE},{Boolean.TRUE},{Boolean.TRUE},{Boolean.TRUE},{Boolean.TRUE},{Boolean.TRUE}};
        String[] header = {"CheckBoxes"};
        public BooleanTable()
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            TableModel model = new AbstractTableModel()
                public String getColumnName(int column)
                    return header[column].toString();
                public int getRowCount()
                    return data.length;
                public int getColumnCount()
                    return header.length;
                public Class getColumnClass(int columnIndex)
                    return( data[0][columnIndex].getClass() );
                public Object getValueAt(int row, int col)
                    return data[row][col];
                public boolean isCellEditable(int row, int column)
                    return true;
                public void setValueAt(Object value, int row, int col)
                    data[row][col] = value;
                    fireTableCellUpdated(row, col);
            JTable table = new JTable(model);
            table.setDefaultRenderer( Boolean.class, new MyCellRenderer() );
            getContentPane().add( new JScrollPane( table ) );
            pack();
            setLocationRelativeTo( null );
            setVisible( true );
        public static void main( String[] a )
            try
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            catch( Exception e )
            new BooleanTable();
        private class MyCellRenderer extends JCheckBox implements TableCellRenderer
            public MyCellRenderer()
                super();
                setHorizontalAlignment(SwingConstants.CENTER);
            public Component getTableCellRendererComponent(JTable
                                                           table, Object value, boolean isSelected, boolean
                                                           hasFocus, int row, int column)
                if (isSelected) {
                   setForeground(Color.white);
                   setBackground(Color.black);
                } else {
                   setForeground(Color.black);
                   if (row % 2 == 0) {
                      setBackground(Color.white);
                   } else {
                      setBackground(new Color(239, 245, 217));
                setSelected( Boolean.valueOf( value.toString() ).booleanValue() );
             return this;
    }

    Instead of extending JCheckBox, extend JPanel... put a checkbox in it. (border layout center).
    Or better yet, don't extend any gui component. This keeps things very clean. Don't extend a gui component unless you have no other choice.
    private class MyCellRenderer implements TableCellRenderer {
        private JPanel    _panel = null;
        private JCheckBox _checkBox = null;
        public MyCellRenderer() {
            //  Create & configure the gui components we need
            _panel = new JPanel( new BorderLayout() );
            _checkBox = new JCheckBox();
            _checkBox.setHorizontalAlignment( SwingConstants.CENTER );
            // Layout the gui
            _panel.add( _checkBox, BorderLayout.CENTER );
        public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
            if( isSelected ) {
               _checkBox.setForeground(Color.white);
               _panel.setBackground(Color.black);
            } else {
               _checkBox.setForeground(Color.black);
               if( row % 2 == 0 ) {
                  _panel.setBackground(Color.white);
               } else {
                  _panel.setBackground(new Color(239, 245, 217));
            _checkBox.setSelected( Boolean.valueOf( value.toString() ).booleanValue() );
            return _panel;
    }

Maybe you are looking for