0

I am trying to make all JLabels opaque by default with a custom look and feel. I can set things like foreground (Label.foreground) but how do you set the opaque property?

EDIT#1: I am creating a custom look & feel that extends MetalLookAndfeel. I override the initComponentDefaults(UIDefaults) method. However, I am open to other ways that involve using a look & feel.

EDIT#2: I tried Vince Emigh's suggestion but it did not work. It appears that the opaque property on a JLabel has a bug - see make-jlabel-backround-transparent-again.

EDIT#3:Code sample

class Demo {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("Label.opaque", Boolean.valueOf(true));
                UIManager.put("Label.background", new ColorUIResource(Color.RED));

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(3);

                JLabel yoyo = new JLabel("YOYOYOYO");
                yoyo.setOpaque(true);// try once with this commented out and note difference

                JPanel panel = new JPanel();
                panel.setSize(200, 200);
                panel.setBackground(Color.BLUE);
                panel.add(yoyo);

                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

I also tried to set the opaque property of a JPanel - same results. It appears the setting the opaque property using UIManager has no effect on any object. :(

Is there another way to do this?

Community
  • 1
  • 1
BigMac66
  • 1,528
  • 5
  • 19
  • 35

1 Answers1

0
UIManager.put("Label.opaque", Boolean.valueOf(true));

It's the same as any other property


class Demo {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("Label.opaque", Boolean.valueOf(true));

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(3);

                JPanel panel = new JPanel();
                panel.setSize(200, 200);
                panel.setBackground(Color.BLUE);
                panel.add(new JLabel("YOYOYOYO");

                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
Vince
  • 14,470
  • 7
  • 39
  • 84
  • I will have to try this out. The reason I haven't done so before is that when I look at a list of Metal's L&F properties for Label using [link](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) I do not see "opaque" listed. – BigMac66 Sep 03 '14 at 19:54
  • @BigMac66 I'm not sure what list you're referring to (didn't see a list in the link), but I tested this before I posted. Let me know if you get the results you are looking for – Vince Sep 03 '14 at 19:57
  • Unfortunately it did not work. I am running java version 1.7.0_21 on a Windows 7 computer. BTW there is no list at the site my link points to - there is a demo app that I find useful for investigating various L&F - they are not all the same! Set the L&F to Metal and select Label and you will see all the defaults for JLabel. – BigMac66 Sep 04 '14 at 13:38
  • could you post the code you use to test - I want to see if I am doing something differently than I should. - Thanks. – BigMac66 Sep 08 '14 at 15:48
  • @BigMac66 Sorry, I ended up deleting the test code, and I'm on vacation, bit I wrote up a little demo. Keep in mind, Swing uses `Metal` by default – Vince Sep 09 '14 at 01:51
  • when I run your code with the label property set to true and the panel blue I see black letters on blue. When I call setOpaque(true) directly on the label I see black letters in a white rectangle surrounded by blue. To me it still looks like the label is transparent until I create an instance of one and call setOpaque() directly. – BigMac66 Sep 12 '14 at 14:17
  • @BigMac66 I'm not quite sure what you mean. Black letters on blue is what you wanted, is it not? (allowing background to be visible behind component without that white rectangle). I'm not sure if we're on the same page. – Vince Sep 12 '14 at 14:54
  • I added some code that I believe shows that when you set the opaque property on a label nothing happens. Other properties seem to work but for some reason not this one. Is there another way to create a L&F that will get the opaque property on label to work? – BigMac66 Sep 12 '14 at 16:02