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?