-1

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())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }

    String username = JOptionPane.showInputDialog(batFrame, "Enter something...:");
    }
}
Cody
  • 870
  • 4
  • 21
  • 38
  • 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
  • How is the right one correct given that you want the buttons to be black with Color(0, 0, 0)? – NickLH Oct 27 '11 at 20:24
  • 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](http://sscce.org/), 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 03:14
  • I added a sample code which doesn't even load the button colors. This is the exact same one that loads them about half of the time(I actually counted) on my program :S. You can see that it loads the background and other things which are written the same way. – Cody Oct 28 '11 at 23:31
  • I ran this on my MacBook; the buttons *never* change color, and I suspect this is by design. What platform are you on? – Ernest Friedman-Hill Oct 28 '11 at 23:36
  • exactly the same happens to me with that code, but its identical to the other one im using (except that one is much much bigger since there is other parts to it). And in that one it only changes colors half the time :S I have no idea why its doing this. I used the right parameters as posted here: http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html ... I have no idea what the problem is. Im using windows (7 64-bit) – Cody Oct 28 '11 at 23:52

3 Answers3

0

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:

myButton.setBackground(Color.BLACK);

and that usually does the trick.

Globmont
  • 881
  • 2
  • 8
  • 20
  • 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 00: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
0

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

SwingUtilities.updateComponentTreeUI(<your_root_ui_componenet>);
Richard Walton
  • 4,789
  • 3
  • 38
  • 49
  • 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 00: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 01:35
0

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.

Cody
  • 870
  • 4
  • 21
  • 38