1

I have e.g.

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("text",Color.GREEN);`

The text is still black, but why?.

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


public class test999 extends JFrame {
  private JLabel jLabel1 = new JLabel();

  public test999(String title) { 
    super(title);
    try {
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          UIManager.setLookAndFeel(info.getClassName());
          UIDefaults defaults = new UIDefaults();
          defaults.put("text",new Color(255,0,0));
          break;
        }
      }
    } catch (Exception e) {
      // If Nimbus is not available, you can set the GUI to another look and feel.
    }
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 300; 
    int frameHeight = 300;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);

    jLabel1.setBounds(72, 72, 147, 57);
    jLabel1.setText("text");
    cp.add(jLabel1);

    setVisible(true);
  } 
  public static void main(String[] args) {
    new test999("test999");
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

That's not how UI defaults work: "text" is not a valid name, and no component can see your defaults instance. Instead, try

jLabel1.setForeground(Color.red);

Also, don't use setBounds(); use a suitable layout manager.

Addendum: As shown here, "text" is a valid primary color key, not a component key.

I…want to…override the nimbus default.

On most L&F's you can specify the "Label.foreground" key:

UIManager.put("Label.foreground", Color.red);

On Nimbus you have to do this:

UIManager.put("text", Color.red);
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I do not want to set the font/textcolor for every single component with one line per component but override the nimbus defaults, this works now: http://stackoverflow.com/a/2576230/753676 but only for text and not the progressbar –  Apr 30 '13 at 11:37
  • isnt this the same as default.put ... from my initial post? –  Apr 30 '13 at 11:53
  • +1 but no idea why, it doesn't works on WinOS, Java6/7, for JLabel, no one from keys that I used, could be bug :-) – mKorbel Apr 30 '13 at 11:59
  • well, the code from my initial post works. but defaults.put("Menu.foreground",new Color(255,0,0)); not –  Apr 30 '13 at 11:59
  • didn't works on my PC, Win8 Java6/7 (then on Win7 probably too) – mKorbel Apr 30 '13 at 12:01
  • I'm talking about JLabel only, rest isn't an issue, keys are hidden in component tree – mKorbel Apr 30 '13 at 12:02
  • No, your `new UIDefaults()` is empty and otherwise unused. You want to modify the [developer defaults](http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html). – trashgod Apr 30 '13 at 12:03
  • @mKorbel: I defer to your greater Nimbus experience; I'd welcome correction or a better answer. – trashgod Apr 30 '13 at 12:07
  • 2
    works now, the other things are painters so I have to recreate them with my custom painter –  Apr 30 '13 at 12:08
  • I don't know answer for JLabels foreground :-), maybe offical bug, maybe somewhere on www.sites – mKorbel Apr 30 '13 at 12:14