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]

# 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?
# 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.
# 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.
# 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.