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.

Similar Messages

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

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

  • Can I change the back/foreground color of a disabled JComboBox?

    I need to change the color of the text or the background for a disabled JComboBox. My users would like to be able to see the data better even though they can't alter it.

    I see i have to create a ComboBoxUI, but have no idea how to start a simple one.Then start with an existing one.
    a) look at the source code for BasicComboBoxUI
    b) find the occurences of 'disabledBackground' and 'disabledForeground' to give you an idea of where to make your changes and then override those methods
    Thats the easy part. If you run this [url http://www.discoverteenergy.com/files/ShowUIDefaults.java]program you will notice that the Metal LAF uses the      javax.swing.plaf.metal.MetalComboBoxUI class and the Windows LAF uses the com.sun.java.swing.plaf.windows.WindowsComboBoxUI class. So if you need to support both LAFs then you would need to override both classes and tell the LAF Manager which class to use. I don't know how to do this.

  • 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

  • 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

  • How to adapt icon colors to component's foreground color?

    I want to display a transparent png-icon on a JButton. How can i change the color of non-transparent (foreground-)pixels in the png-icon to the foreground color of the JButton?

    You'll need to study up some on basic CSS terms. 
    CSS Tutorial
    Align is invalid code.  What you probably want is:
         text-align:left;
         text-align:center;
         text-align:right;
    Clear is only applicable to floated elements.   It doesn't remove colors.
    footer {
         color: #FFF; /**this is white text**/
         background-color: #000; /**this is black background**/
    /**or**/
         background:none;
    Nancy O.

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

  • 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

  • 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 The Text Color In a Radio Button Component

    Hi,
    I'm using a background in my frame , so I need to know how
    to change the color of the Radio Button Component. I tried using
    the Property Inspector , but in that the color option is disabled.
    The Flash version I'm using is Flash 8. Kindly provide me a
    solution for this.
    Thanks In Advance,
    Lokesh R

    There's a topic about this in the Flash help, about changing
    components. You need to edit the file inside your Flash
    installation folder, and make a copy of it to your liking.
    But about radio buttons: is there any way to include them in
    your project while maintaining the possibility of on(Keypress)
    events?

  • How to remove the border from an AWT Component (specifically a ScrollPane)

    I've been trying to remove the border from an AWT ScrollPane, but having no luck.
    I've tried setting the bounds on the ScrollPane to get rid of the border:
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setBounds(-2, -2, getSize().width+4, getSize().height+4);
    I've also tried overwriting the paint method of the ScrollPane but it didn't draw anything:
    private class CustomScrollPane extends ScrollPane
         public void paint(Graphics p_g)
              super.paint(p_g);
              Graphics2D g2 = (Graphics2D)p_g.create();
              g2.setColor(Color.MAGENTA);     //tried magenta so I could easily see it
              g2.drawLine(0, 0, 0, getSize().width);
    Is there anything else I should try? (I wish AWT components had a setBorder method)

    Is there anything else I should try? (I wish AWT components had a setBorder method)Actually this probably isn't possible. One of the main drawbacks of AWT was that since everything is connected to peers, you can't actually draw over or customize the view of your AWT component.
    The only recommendation I might make would be that you could try creating Canvases around your ScrollPanel that draw over the border

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

  • JTable specific row foreground color

    I know how to set a foreground color of a TableCell this isn't the problem.
    The problem comes when I want to have a specific row no.

        private class SimpleRenderer implements TableCellRenderer {
            JLabel label;
                label = new JLabel();
                label.setOpaque(true);
            public Component getTableCellRendererComponent(JTable table, Object value,
                                                           boolean isSelected, boolean hasFocus,
                                                           int row, int column) {
                label.setText(value != null ? value.toString() : "");
                if (row == 0) {
                    label.setBackground(Color.red);
                } else {
                    label.setBackground(Color.blue);
                return label;
        }

  • Changing custom AWT component to Swing component

    I created my own AWT component. I'd like to change it to Swing component.
    Does anyone know how to change or create a custom Swing component?
    Please also provide some framework to develop a custom Swing component.

    First try to know the difference between SWING & AWT. You can either adopt to SWING or AWT but be specific of what you want to do !
    If you like to migrate from AWT to SWING ,then make the necessary changes in your coding.(ie, re frame things like , Frame to JFrame , Button to JButton ,JTextField to JTextField etc..)
    For creating custom components ,say a component called MyLabel and you should design a class which extends JLabel and do whatever you like...
    eg import javax.swing.*;
    public class MyLabel extends JLabel
      MyLabel(String title)
         this.setTitle(title);
         this.setBackground(Color.white);
         this.setForeground(Color.blue);
    }For more details go to java tutorials.

Maybe you are looking for

  • Stub(Proxy) generation for external Web Service in SAP NW WebAS 6.40

    Hi, Can anyone tell me what tool is used to generate the proxy files for accessing any external web services in SAP NetWeaver WebAS 6.40 for JAVA (SAP DevStudio 2.0.5). Do i have to install any components or configuration for using external web servi

  • Can't download or update on app world getting error 0003-0005

    Was able to download a few apps when the o's 2.0 came out. Now getting error posted above. I tried the date change workaround, that didn't work. I tried checking the blackberry id and getting the error stating that I am not connected to Wifi. I am co

  • RV016 - QoS questions

    Good evening everyone I'm a network administrator and I have been struggling with QoS and I will explain why. My RV016 is directly connected to the modem and to a gigabit switch as well. there's only one cat6 carrying the traffic to the switch, which

  • Problem installing/updating iTunes x64

    Hello everyone, I tried to upgrade my iTunes to the latest version but I get errors during the installation. I get the error two times. I've searched for similar problems but the solutions that I found didn't helped me. I tried everything but I can't

  • Pdf from word

    hi i have this problem. I create cmyk images from photoshop. When i place them in word and export this file to pdf the seperation is not correct. I can see only 3 colours. Magenta, yellow and blue. Black appear only on letters not in the images. than