Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:09:32+00:00 2026-06-04T05:09:32+00:00

While using the Nimbus L&F feel in Java, I am having problems with changing

  • 0

While using the Nimbus L&F feel in Java, I am having problems with changing the background color for a JButton more than once. In the code below, I have a simple Swing application that displays a JButton and attempts to change the color once per second. However, only the first color is being applied. Can anyone provide any suggestions on how to make this change more than once? I’m running java 6.29.

public class NimbusTest3 extends JFrame {
    private javax.swing.JButton button;

    public NimbusTest3(){
        button = new javax.swing.JButton();
        button.setText("Text");
        this.add(button, BorderLayout.CENTER);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.pack();

        Timer t = new Timer(1000, new ActionListener() {
            Random r = new Random();
            @Override
            public void actionPerformed(ActionEvent e) {
                UIDefaults buttonDefaults = UIManager.getLookAndFeelDefaults();
                Color c = new Color(r.nextInt(
                        256), r.nextInt(256), r.nextInt(256));
                System.out.println(c);
                buttonDefaults.put("Button.background", c);
                button.putClientProperty("Nimbus.Overrides", buttonDefaults);
                button.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
                SwingUtilities.updateComponentTreeUI(button);
                button.repaint();
            }
        });
        t.start();
    }

    public static void main(String args[]) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            return;
        } 

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NimbusTest3().setVisible(true);
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:09:34+00:00Added an answer on June 4, 2026 at 5:09 am
    • for Nimbus you have to call SwingUtilities.updateComponentTreeUI(myButton); after any of changes for Nimbus Defaults

    • more infos about that in this thread about Background and JPanel

    • read Nimbus Look and Feel

    EDIT :

    I agreed not possible to change that direct way (maybe there is another dirty hacks) is possible that with very complicated way (development of Nimbus L&F ended somewhere on first half), another (similair) issue is in my question about Font

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.LookAndFeel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIDefaults;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class NimbusTest3 extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private javax.swing.JButton button;
    
        public NimbusTest3() {
            button = new javax.swing.JButton();
            button.setText("Text");
            add(button);
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            this.pack();
            Timer t = new Timer(1000, new ActionListener() {
    
                private Random r = new Random();
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
                    try {
                        LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
                        UIDefaults uiDefaults = lnf.getDefaults();
                        uiDefaults.put("nimbusBase", c);
                        UIManager.getLookAndFeel().uninitialize();
                        UIManager.setLookAndFeel(lnf);
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
                    UIDefaults defaults = UIManager.getDefaults();
                    defaults.put("Button.background", c);
                    SwingUtilities.updateComponentTreeUI(button);
                }
            });
            t.start();
        }
    
        public static void main(String args[]) {
            try {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (Exception e) {
                return;
            }
    
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new NimbusTest3().setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While using sql 2005, it appears I am unable to insert more than 8000
while using tiles along with spring i m getting following error: java.net.UnknownHostException: tiles.apache.org program
while using window.print() the background colors for table cells not printing Thanks for any
While using Java's switch case, it excepts only char and int, but I want
hello i am using the nimbus look-and-feel and have a tabbedpane with an icon
While using the .NET Color struct, I found myself wondering why Microsoft chose to
While using UIVIEW I put an image to the background and added a UIBUtton,
While using CA Siteminder's JAVA SDK API we noticed a starnge issue that sometimes
While using the GWT application, I deployed the new version of the application. Once
I'm using Nimbus Look and Feel, with only 3 changes at its colors: UIManager.put(nimbusSelection,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.