Search code examples
javaswinglook-and-feelnimbus

JTextArea background issue


private JDialog dialog;
private JTextArea text;
private JPanel buttons, filler;
private JRadioButton questions, list;
private ButtonGroup group;
private JButton confirm;

dialog = new JDialog(Main.masterWindow, lang.getString("newTitle"), true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
dialog.setResizable(false);

text = new JTextArea();

//this works
text.setBackground(Color.RED);

//this both don't
text.setBackground((Color)UIManager.get("control");
text.setBackground(dialog.getContentPane().getBackground());

dialog.setVisible(true);

I am using Nimbus L&F, and "control" is the background color of my dialog. If I set any other color (red in this example) it shows, but if I set it to this one, it's stays white.

I don't have tis problem on default (metal) L&F...

What's the problem?


Solution

  • For some reason, it doesn't seem to like the ColorUIResource object the is returned from UIManager.get call. I can't see why, because it's derived from Color.

    If you do something like

    JDialog dialog = new JDialog((JFrame) null, "Help", true);
    dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
    
    JTextArea text = new JTextArea(10, 10);
    
    Color color = new Color(UIManager.getColor("control").getRGB()); // <-- Create a new color
    
    text.setBackground(bg);
    
    dialog.add(text);
    dialog.pack();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
    

    This seems to work.

    Should you have to do it. I don't think so, but every thing else I tried didn't work