Results 1 to 5 of 5
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default How to set background of a disabled JButton?

    Hello.

    I am trying to use two backgrounds in one application: one dark and one light. When I select the dark background, I cannot control the color of disabled button. It becomes dark gray. I tried:

    Java Code:
    myButton.setBackground(new Color(255,255,255));
    but it does not help. Is there any way to adjust the background of the JButton when it is disabled? Thanks a lot!

    How to set background of a disabled JButton?-screen-shot-2018-01-13-1.26.15-am.png

    How to set background of a disabled JButton?-screen-shot-2018-01-13-1.26.57-am.png

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: How to set background of a disabled JButton?

    Ok, I was bored so I wrote this. Perhaps you can find something useful in it. It simply
    oscillates between enabled and disabled every 1.5 seconds. Next time, please
    include a short working example of the problem that can be compiled and run.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Insets;
    
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    public class Background extends  JPanel{
     JFrame frame = new JFrame("Button Demo");
     
     public Background() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300,70));
        frame.add(this);
        JButton b = new JButton("Enabled");
        b.setBackground(Color.GREEN);
        b.setFont(new Font("Arial", Font.BOLD, 20));
        
        b.setPreferredSize(new Dimension(150,50));
        b.setFocusPainted(false); // get rid of border around text
        add(b);
        Timer t = new Timer(0, (e)->{
           if (b.isEnabled()) {
              b.setEnabled(false);
              b.setBackground(Color.RED);
              b.setText("Disabled");
           } else {
              b.setEnabled(true);
              b.setText("Enabled");
              b.setBackground(Color.GREEN);
           }
        });
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        t.setDelay(1500);
        t.start();
     }
       public static void main(String[] args) {
           new Background();
       }
    
    
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: How to set background of a disabled JButton?

    Thank you, Jim. For some reason, the setBackground() method did not work for me.

    Anyways, I managed to solve the issue by creating a custom class ButtonPainter implementing Painter and then doing the following:

    Java Code:
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    UIManager.getLookAndFeelDefaults().put("Button[Disabled].backgroundPainter", new ButtonPainter(Color.YELLOW, Color.RED));
    It actually works now. However, I have a question. Can I have the UIManager Look and Feel change instantly, say by clicking on a button. Here is a code. I would like to change the background colour by clicking on a button. It works only if I click once -- the second time it does not work. I do not want to use something like panel.setBackground() -- I do want to use UIManager.

    Java Code:
    package javaapplication30;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    
    public class JavaApplication30 extends  JPanel
    {
        JFrame frame = new JFrame("Button Demo");
      
        public JavaApplication30() 
        {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(300,300));
            frame.add(this);
            
            JButton b1 = new JButton("BLACK");
            b1.setPreferredSize(new Dimension(150,50));
            b1.setFocusPainted(false); // get rid of border around text
            add(b1);
            b1.addActionListener((java.awt.event.ActionEvent evt) -> 
            {
                try 
                    {
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    
    
                        UIManager.getLookAndFeelDefaults().put("Panel.background", Color.BLACK);
                        
                        SwingUtilities.updateComponentTreeUI(frame);
                        frame.invalidate();
                        frame.validate();
                        frame.repaint();
                        frame.pack();
                    }
                    catch ( ClassNotFoundException | 
                            InstantiationException | 
                            IllegalAccessException | 
                            UnsupportedLookAndFeelException ex) 
                    {
                        
                    }
            });
            
            JButton b2 = new JButton("RED");
            b2.setPreferredSize(new Dimension(150,50));
            b2.setFocusPainted(false); // get rid of border around text
            add(b2);
            
            b2.addActionListener((java.awt.event.ActionEvent evt) -> 
            {
                try 
                    {
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    
    
                        UIManager.getLookAndFeelDefaults().put("Panel.background", Color.RED);
                        
                        SwingUtilities.updateComponentTreeUI(frame);
                        frame.invalidate();
                        frame.validate();
                        frame.repaint();
                        frame.pack();
                    }
                    catch ( ClassNotFoundException | 
                            InstantiationException | 
                            IllegalAccessException | 
                            UnsupportedLookAndFeelException ex) 
                    {
                        
                    }
            });
            
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            
        }
        
        
        public static void main(String[] args) 
        {
           new JavaApplication30();
       }
     
    }
    If you know, I would greatly appreciate your answer!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: How to set background of a disabled JButton?

    Did it work when you tried it? I have not tried to change the Look and Feel via a button. You might check out the tutorials to see if they cover it.
    Also, not certain why your setBackground didn't work. Perhaps you did something subtle and it is causing some problems.

    Edit: BTW, I actually don't like setting properties in the UI classes unless the properties are defined as part of the official documentation. For most of them
    I can't find them except in the source and unofficial sites. Anything which is not part of the doc on the classes may be changed at any time
    which means your code could possibly break in the future. That also goes for overriding methods since the internals of those methods could change.
    So I would rely only on the Java API javadoc and nothing else when developing applications.

    Regards,
    Jim
    Last edited by jim829; 01-14-2018 at 03:50 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: How to set background of a disabled JButton?

    Jim. I believe that changing properties of the UI components of Nimbus Look and Feel is documented and supported.
    In any case, if someone could please point to the culprit in my second code, I would greatly appreciate it. Labels are changed, but panel background does not change and I do not understand why.
    Cheers,
    Igor

Similar Threads

  1. how to enable only text on disabled jButton?
    By sandeep43 in forum NetBeans
    Replies: 6
    Last Post: 06-22-2011, 07:34 AM
  2. JButton : Background and Text
    By Keys in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 04:43 AM
  3. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 06:55 PM
  4. Replies: 4
    Last Post: 03-21-2009, 05:48 PM
  5. Disabled JButton font color
    By p900128 in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2008, 08:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •