Disabled foreground color in JComboBox

what's the correct way to set the disabled foreground color of a jcombobox? i don't want to set the disabled color for the entire ui to the same color, but for one jcombobox specifically. i searched the web and found many posts with the same question, but no real solution. this one has been giving me nightmares and i thought i'd share what i've found. the solution seems to be quite simple: wrap the combobox label in a panel. that way the panel gets the disabled colors, but the coloring of the label of the combobox remains. here's the solution in short in case others need it:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
public class MyComboBox extends JComboBox {
  public MyComboBox() {
    super();
    setRenderer( new ColorCellRenderer());
  public static void main(String s[]) {
    JFrame frame = new JFrame("ComboBox Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JComboBox cb = new MyComboBox();
    cb.addItem( "ComboBox Item 1");
    cb.addItem( "ComboBox Item 2");
    cb.addItem( "ComboBox Item 3");
    cb.setForeground( Color.blue);
    final JButton bt = new JButton ("disable");
    bt.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        cb.setEnabled( !cb.isEnabled());
        if( !cb.isEnabled()) {
          cb.setForeground( Color.red);
          bt.setText( "enable");
        } else {
          cb.setForeground( Color.blue);
          bt.setText( "disable");
    frame.getContentPane().setLayout( new FlowLayout());
    frame.getContentPane().add( bt);
    frame.getContentPane().add( cb);
    frame.pack();
    frame.setVisible(true);
  class ColorCellRenderer implements ListCellRenderer {
    protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
      JLabel label = (JLabel) defaultRenderer
          .getListCellRendererComponent(list, value, index,
              isSelected, cellHasFocus);
      JPanel wrapper = new JPanel();
      wrapper.setLayout( new BorderLayout());
      wrapper.add( label, BorderLayout.CENTER);
      return wrapper;
}if anyone knows a way to remove the dropdown button of a disabled combobox without leaving a blank space when you simply hide the button, feel free to extend this :-)
Edited by: Lofi on Mar 20, 2010 10:46 PM

i thought i'd share what i've found. the solution seems to be quite simple: wrap the combobox label in a panel.Brilliant! I'm sure this will be useful to others who find this solution here.
Your code does have some unnecessary overheads though, like constructing a new JPanel in every call to getListCellRendererComponent, and holding a separate renderer as a field. I've attempted to clean it up for brevity and efficiency.import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisabledForegroundListCellRenderer extends DefaultListCellRenderer {
  private final JPanel wrapper = new JPanel(new BorderLayout());
  public DisabledForegroundListCellRenderer() {
    wrapper.add(this);
  @Override
  public Component getListCellRendererComponent(JList list, Object value,
          int index, boolean isSelected, boolean cellHasFocus) {
    super.getListCellRendererComponent(list, value,
            index, isSelected, cellHasFocus);
    return wrapper;
  // Test rig
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new DisabledForegroundListCellRenderer().makeUI();
  public void makeUI() {
    String[] data = {"One", "Two", "Three"};
    final JComboBox combo = new JComboBox(data);
    combo.setRenderer(new DisabledForegroundListCellRenderer());
    final JCheckBox check = new JCheckBox("Combo enabled");
    check.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        boolean selected = check.isSelected();
        combo.setForeground(selected ? Color.BLUE : Color.RED);
        combo.setEnabled(selected);
    check.doClick();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.add(check, BorderLayout.NORTH);
    frame.add(combo, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}I've also changed the class name, as to me "ColorCellRenderer" would rather imply something that renders either the foreground or the background (or both) on the basis of the value parameter.
if anyone knows a way to remove the dropdown button of a disabled combobox without leaving a blank space when you simply hide the buttonThat would require a custom UI delegate, not a custom renderer. And really, I wouldn't bother -- I would just use a JComboBox and a disabled (or maybe uneditable) JTextField, held in a CardLayout, and swap them instead of disabling the combo.
Shall take a look at how the combo UIs reserve space for the button though :)
luck, db
Edited by: DarrylBurke -- changed wrapper to private final, it shouldn't be static

Similar Messages

  • Changing the disabled foreground color of a disabled JList

    I am using a JList inside JScrollPane. If the list is disabled the foreground color of list changes to grey. How can I change this disabled foreground color.
    regards,
    nirvan.

    Well, a JList uses a JLabel as the default renderer. So you can try using the UIManager to set the "Label.disabledForeground" color to whatever you want. Of course this will also affect disabled labels.
    Or, you can create a custom renderer for the list and control the disabled color in the renderer.

  • 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

  • Foreground color of a disabled component

    How do i change foreground/text color of a disabled component? Something similar to JTextComponent's setDisabledTextColor().

    I'm not exactly sure but I think this is highly component dependent and even look & feel dependent. Sometimes, there isn't even a such color defined anywhere. The component's ui just takes the current foreground color and calls brighter on it.
    You'll have to be more specific on the component you wish to modify.

  • Foreground Color of a Disabled AWT Component

    How do you change the foreground color of a disabled AWT component?

    You would have to write your own renderer that checkes to see if the component is enabled or disabled. Your renderer would then draw the component as you like in both of those states.

  • Is there a way to disallow web pages to modify foreground color of elements if background of those elements was not changed as well (and vice versa)?

    I have very sensible defaults for my display; i.e. I have dark GTK scheme (dark gray background, light gray text) that suits me way more when working on my computer while it's dark around. I, however, like looking at web pages as how they were mean to be seen. Unfortunately, most of web designers don't think about anyone having non-default colors set up.
    Example one ( http://www.cedok.cz ):
    This site's CSS style has "color: rgb(45, 45, 45);" for the whole <body> but for <select> elements there is no "background-color" set. This means there is a select box rendered with dark gray background and dark gray foreground (text color). This is pretty badly readable (obviously).
    To mention one particular element from this site: e.g. <select id="ctl00_ContentPlaceHolderLevySloupec_HledaniMultiTab1_HledaniZajezdu1_ddlTypZajezdu">
    Example two ( https://support.mozilla.org/en-US/questions/new/desktop/customize/form ):
    On this page (where I'm currently filling in this question) the <textarea id="id_content"> has "background: ... rgb(255, 255, 255);" set, but with my default font color, this textarea is rendered as light gray text on a white background. Again, pretty unreadable (and eye-hurting).
    I can provide screenshots and more reasoning if needed.
    I'm asking if there is (or might be) an option (even disabled by default) or at least an add-on which would keep default colors unless both foreground and background colors were specified. I think this might make Firefox very usable for many people which like to have their default colors configured.
    In case you won't be able to help me by fixing this, could you redirect me to the right place where I could get this requested in a way that might be really possible to happen (bugzilla?).
    Thank you very much in advance.

    Thanks for the reply, but I'm sorry, no. I don't want any addon that customizes colors, I want to use default colors and only prohibit changing them unless both background and foreground colors are changed.
    I have made no color changes WRT to Firefox *only*, I just changed the default for the whole toolkit (gtk in this case). The thing I need is to disable changing *only one* property out of two ({back,fore}ground). Whenever the second one gets set as well, this new setting (both of them) may get reflected, but not before that. It'd be nice to keep this when changing the colors back (deleting the style), but that's not necessary.
    I hope that's understandable, feel free to ask for if that's not the case.
    To express what I mean a bit more precisely, see the attached image. In that image there are four textareas (one of the elements that gets affected by this). First one is a default one (which I want to have if the page has nothing set), the second one is nicely modified to fit for example to a blue-ish page (this one I like as well).
    Second row of textareas shows how the final element looks like if the page has only one of these elements set up. First one changes background without changing font color, the second one vice versa. Both of them are very badly readable, especially with low lighting around.
    This is consequently the same case when it comes to default page background which I cannot set at all, because too many pages have only "color:" set without changed background and that is completely messed up then. Going to extremes, I might mention all color-related modifications (see how ugly the borders look when the background is changed and nothing is done to these borders, their size and color is kept default), but I understand that's too much.

  • Changing JCheckbox's foreground color..

    Hi Guys...
    I want to change the foreground color of the checkmark that appears in the jcheckbox when it is checked.. Is this possible to do? I have read problems with the disabled background color... But I have not found anything on how to change the foreground color of the checkmark when its checked... I tried doing "setForeground(Color.red)" but it does not work..... Thanks alot in Advance...

    I tried the change color when selected, it doesn't seem to work. However, you can change the icons used on a JCheckbox, the appended code does just that, it uses a collection of CD Player icons for the example, but you could draw or load anything you want. You might also find the CD icons useful for other purposes.
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class TestChk extends JFrame
         Container cp;
         JCheckBox c2;
         TestChk()
              cp = getContentPane();
              addWindowListener(new WindowAdapter()
              {public void windowClosing(WindowEvent evt){System.exit(0);}});
             JCheckBox cb = new JCheckBox();
             CdIcon ic = new CdIcon(CdIcon.CdiStop,16,16,Color.BLACK);
             cb.setIcon(ic);
              CdIcon sic = new CdIcon(CdIcon.CdiPlay,16,16,Color.RED);
              cb.setSelectedIcon(sic);
              JPanel p = new JPanel();
              p.add(cb);
              cp.add(cb,BorderLayout.NORTH);
              pack();
         static public void main(String[] args){new TestChk().show();}
    class CdIcon implements Icon
         public static final int CdiStop = 0;
         public static final int CdiPlay = 1;
         public static final int CdiPause = 2;
         public static final int CdiRewind = 3;
         public static final int CdiFastBack = 4;
         public static final int CdiFastForward = 5;
         int type;
         int height, width;
         int iheight, iwidth;
         int left, top;
         Color clr;
         CdIcon(int t, int h, int w, Color c)
              type = t;
              height = h;
              width = w;
              iheight = (int)(.75*(float)height);
              iwidth = (int)(.75*(float)width);
              top = (int)(.125*(float)height);
              left = (int)(.125*(float)width);
              clr = c;
         public int getIconWidth(){return width;}
         public int getIconHeight(){return height;}
         public void paintIcon(Component c, Graphics g, int x, int y)
              int itop, ileft;
              itop = y + top;
              ileft = x + left;
              int[] xp;
              int[] yp;
              int xd;
              if(c.isEnabled())g.setColor(clr);
              else g.setColor(Color.gray);
              switch(type)
                   case CdiStop:
                        g.fillRect(ileft,itop,iwidth,iheight);
                        break;
                   case CdiPlay:
                        xp = new int[]{ileft,ileft+iwidth,ileft};
                        yp = new int[]{itop,(itop+(iheight/2)),itop+iheight};
                        g.fillPolygon(xp,yp,3);
                        break;
                   case CdiPause:
                        xd = iwidth /4;
                        g.fillRect(ileft,itop,xd,iheight);
                        g.fillRect(ileft+iwidth-xd,itop,xd,iheight);
                        break;
                   case CdiFastForward:
                        xd = iwidth /2;
                        xp =new int[]{ileft,ileft+xd,ileft};
                        yp =new int[]{itop,(itop+(iheight/2)),itop+iheight};
                        g.fillPolygon(xp,yp,3);
                        xp =new int[]{ileft+xd,ileft+xd+xd,ileft+xd};
                        g.fillPolygon(xp,yp,3);
                        break;
                   case CdiFastBack:
                        xd = iwidth /2;
                        xp =new int[]{ileft+xd,ileft,ileft+xd};
                        yp =new int[]{itop,(itop+(iheight/2)),itop+iheight};
                        g.fillPolygon(xp,yp,3);
                        xp =new int[]{ileft+xd+xd,ileft+xd,ileft+xd+xd};
                        g.fillPolygon(xp,yp,3);
                        break;
                   case CdiRewind:
                        int xm = iwidth/8;
                        int xleft = ileft+xm;
                        xd = (iwidth-xm)/2;
                        g.fillRect(ileft,itop,xm,iheight);
                        xp =new int[]{xleft+xd,xleft,xleft+xd};
                        yp =new int[]{itop,(itop+(iheight/2)),itop+iheight};
                        g.fillPolygon(xp,yp,3);
                        xp =new int[]{xleft+xd+xd,xleft+xd,xleft+xd+xd};
                        g.fillPolygon(xp,yp,3);
                        break;
                   default:
                        g.drawRect(ileft,itop,iwidth,iheight);
                        break;
    }

  • Changing the foreground color ......

    I don't want the viewer to change the text, so I did
    setEnabled(false);
    on my JTextArea.... but then my text color stays at gray no matter what foreground color I give it.
    Is there anyway to overwrite this?
    Thanks!!

    call setaEditable( false ), that won't disable it just make it un-editable.

  • Foreground color change from inside a class

    Hey,
    For a project I'm working on, I wrote a class that extends awt.Label. In this new class, I had to override the paramString() method to return what I wanted it to. This all works great. What I was wondering, is whether or not it was possible to change the foreGround color of the label from inside the class decleration. I was thinking maybe whenever the paramString() method was called. Any help would be appreciated.
    Thanks,
    Jarrod

    Gday,
    I you use the mothed setForeground(Color c) it should set the foreground colour (color for you Americans) to what you would like.
    see for more info
    http://java.sun.com/j2se/1.3/docs/api/java/awt/Component.html#setForeground(java.awt.Color)
    or
    http://java.sun.com/j2se/1.4/docs/api/java/awt/Component.html#setForeground(java.awt.Color)
    if you have jdk1.4
    You dont need to make this method in your class just call it when you want. ie
    setForground(Color.RED);
    or
    Color c = new Color(255, 0, 0);
    setForeground(c);
    Good luck,
    Jack 573

  • Foreground color gray

    I'm trying to recolor an object by making a Hue/Saturation layer and clipping it to my object I want recolored. I select the color I want, create the new layer and every time I do that, the selected foreground color reverts to gray and I cannot recolor my object to the desired color. I tried selecting the color after and it remains gray. A few days ago I was able to follow these same steps to recolor. I'm not sure what happened to cause this problem. I am using PE 8 on a Mac. Thanks for your help.

    Do you have the colorize checkbox turned on? Is the Image RGB and not indexed color or anything like that?
    It's not really necessary to clip the layer, you know. You can start by selecting the object, then the layer you create will only include that object, if there was an active selection when you made the adjustment layer.

  • Foreground color turns grey

    Hello
    I am just learning Photoshop CS4 on a Mac.
    I selected a color, expecting the foreground color to change to that color so I could fill a an area. I found that the foreground color changed to grey. Whatever I tried it just went grey. Could someone tell me what is happening here. I am doing something wrong but I have no idea what it is.
    Thanks for any help with this.

    Image menu->Image mode->RGB color
    You appear to be in grayscale mode now.

  • Background and foreground colors can be interchanged using which command

    hi
    background and foreground colors can be interchanged using which command

    <b>Background and foreground colors can be interchanged using the command Format Inverse.</b>

  • 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!

  • Make Eyedropper Set Foreground Color not Background

    How do you make color selections/sampling like with the Eyedropper set the Foreground color?
    It was always that way before CS3 unless I accidentally changed a pref unknowingly.
    Now when I select/sample color it sets the background color.
    I looked, but couldn't find a pref setting for this.
    Thanks

    I think Kath meant to point you toward this entry in the FAQ section:
    http://forums.adobe.com/thread/421402?tstart=0

  • Selective replace foreground-color with background-color

    Hi,
    I'm new to Photoshop and coming from paint shop pro (psp).
    Now I have to recolor a black dotted arrow in a bitmap to red.
    All other contents are black, too. In psp there is a tool/brush to
    replace foreground-color with background-color. So I'd select
    black as foreground-color and red as background-color, hover
    with cursor over the area to recolor and that's it.
    How to do it with Photoshop?
    Thanks in advance
    Jörg

    softmonauts wrote:
    Now I have to recolor a black dotted arrow in a bitmap to red.
    All other contents are black, too.
    Can you clarify this?  It sounds like you have a black arrow on a black background.

Maybe you are looking for