I've made a gui for my program and used

UIManager.put("Button.background", new Color(0,0,0));   
UIManager.put("JButton.background", new Color(0,0,0));

to make the buttons appear black. Unfortunately, this doesn't work sometimes. Without modifying the code, it will work when I run it one time and wont work another time.

The following image is the same button in the same program after several times I ran it. This happens about 1 in 4 times I run the program.

Buttons (left button is correct and the right one is what sometimes happens while running)

Also, other things like

UIManager.put("control", new Color(15,0,0));

are loading properly. Never had a problem with it and it's loaded at the same time and same format.

EDIT: Here's a sample code where the button colors aren't loading at all while the background and other things are. They are loaded the same exact way and there are no compilation or run errors.

import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager.*;

public class gtst


public static void main(String[] args) throws Exception

    UIManager.put("Button.background", new Color(1,1,1));   
    UIManager.put("JButton.background", new Color(1,1,1));
    UIManager.put("control", new Color(0,0,0)); 
    UIManager.put("text", new Color(255,220,0));

    Frame batFrame = new JFrame("nananananna Batman!");

    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
        if ("Nimbus".equals(info.getName())) {

    String username = JOptionPane.showInputDialog(batFrame, "Enter something...:");
  • 2
    You need to write a small, concise, working code example that demonstrates the problem and post it here. Often when doing this you will even solve your own problem along the way. – Brian Roach Oct 27 '11 at 20:17
  • did i not do that already? The code is right there ... and I described the problem, everything you know is what i know... :( I'm a begginer so I'm not 100% sure what's causing this to happen. – Cody Oct 27 '11 at 20:19
  • 2
    oh god, i dont know my right and left – Cody Oct 27 '11 at 20:25
  • 7
    Whenever you have "random" problems, make sure your GUI code executes on the Event Dispatch Thread, by using SwingUtilities.invokeLater(...). – camickr Oct 27 '11 at 20:35
  • 1
    "everything you know is what i know.." In one reply, it was said "make sure your GUI code.." If you had posted an SSCCE, that person, & others, might have been able to check by looking at it (or compiling it, running it & then looking at the code). As it is, we can only make guesses. – Andrew Thompson Oct 28 '11 at 3:14

How I usually do this is I make a button as a seperate JButton variable and then add that to my panel. Then to set the color of the button I use the code:


and that usually does the trick.

  • yah but its using the pre made frame and doing every thing separately will be a lot of code and slightly inefficient :( – Cody Oct 29 '11 at 0:48
  • Oh are you using netbeans to make this? If so then you can just go into the properties of your button and change that. – Globmont Oct 30 '11 at 15:01

Try using the following code after you have made the changes in the UIManager:

  • I dont think that works :( got these 4 errors: background is not public in java.awt.Component; cannot be accessed from outside package (for Button.background /JButton.background). updateComponentTreeUI(java.awt.Component) in javax.swing.SwingUtilities cannot be applied to (java.awt.Color) (for updateComponentTreeUI for both buttons) :( – Cody Oct 29 '11 at 0:52
  • So you did SwingUtilities.updateComponentTreeUI(batFrame); Just before you show the OptionPane> – Richard Walton Oct 29 '11 at 11:48
  • oh no, i was doing something else, well with that it compiles and runs without errors but it still doesnt load the colors :( – Cody Oct 30 '11 at 1:35

After days of non stop trial and error, I figured out that apparently in my program the MySQL connection was causing the problem. If the MySQL connection was initiated at the start of the program and the color changes were too this cause some sort of a problem (maybe lag?) and the colors wouldn't always load. Maybe it's a problem with the MySQL driver or something else, not sure, but to fix it I just made it connect to MySQL when it needed the connection rather than at the program start.

I'm not sure why the colors didn't load at all in the example I provided.

Your Answer

