getBackground() returns different values between Java 5 and 6

I have a JTextPane on a JPanel and I set it's background color to be the color of the panel:

class MyPanelextends JPanel{

public MyPanel (){

setLayout (new BorderLayout());

JPanel p =new JPanel ();// this is actually a scrollpane in my app.

JTextField staticText =new JTextField ();

staticText.setText ("I'm uneditable text");

staticText.setEditable (false);

staticText.setBorder (null);

staticText.setBackground (getBackground());

p.add (staticText);

add (p, BorderLayout.NORTH);

}

}

On Java 5, this works as I want and the text has the same background as the rest of the window. However, on Java 6 the JTextPane's background is white.

What do I need to do to get this consistent between versions?

Thanks in advance!

-matthew

[1267 byte] By [matthewa] at [2007-11-27 10:44:42]
# 1

Perhaps you are making assumptions about what the default background color of a JPanel is in both versions of Java, presuming it wouldn't change between versions.

Set the panel's background to something you know, and don't rely on some undefined default.

bsampieria at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 2

But I don't really know what color the background is. I want the background of the text panel to be the same color as the rest of the dialog, so tan if it's Windows XP default Blue color scheme, silver if you're using silver, etc.

How can you find out what this color is?

matthewa at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

So I changed the code to be:

jTextPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));

and the problem persists. (Incidentally, I'm making the UI through Netbeans. Not sure if that factors into it or not.) I've made superficial changes to the text to make sure the code is the same in both environments.

matthewa at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 4

NetBeans wouldn't factor into it (most likely)...

The point is that UIDefaults for Panel.background in Java 5 and 6 are not necessarily the same. Presumably, for the same L&F, they might be, but more likely that JPanels are non-opaque by default in a L&F and the BG color matters not.

You might be better off trying to set the text pane to be non-opaque, instead of trying to find the highest non-opaque component's background.

bsampieria at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 5

Woot! Setting opaque to false did the trick.

Thanks!

matthewa at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

I find it hard to believe that setBackground() doesn't work.

I don't use JDK6, so I can't test it but does something simple like textPane.setBackground(Color.RED) work?

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, so others that use JDK6 can test your code to see if they encounter the same problem.

camickra at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

setBackground() works. The problem is in determining the correct color to use.

matthewa at 2007-7-28 20:08:26 > top of Java-index,Desktop,Core GUI APIs...