views:

265

answers:

2

As discussed here, when resizing a Swing application in Vista (and Windows 7, which is what I'm using) you get a black background in the right/bottom corner while Swing's repaint catches up to the changes.

Playing with other applications (Windows Explorer (Native), Firefox (C++?), and Eclipse (Java)) I notice that they all have this same problem - contrary to what the folks in the link above say - but they minimize the problem by having a grey fill color, which is far less visually jarring than the black that appears in Swing.

I'm wondering if there's some way to change this so that Swing behaves like these other applications? I tried setting the background color of the JFrame, but to no avail.

Additional Info Jonas discovered (see his informative answer below) that this is an issue with JFrames, but not AWT Frames - maybe that will help someone figure this out.

Update I know it's not all that critical, but it really seems like there's some way to solve this. Anyone?

+1  A: 

I have noticed the same problem. This color is gray at IE, in Opera it's black, and in Eclipse it's gray. It seam to be more visible in Swing, because it seam to be little slower at repainting and the color is as you said, black. This problem is more visible if you use the upper left corner to resize.

I coded an example and tried to understand where this black color is defined. A JFrame has many layers, so I set a different background on every layer.

import java.awt.Color;
import javax.swing.JFrame;

public class BFrame {

    public static void main(String[] args) {
        new JFrame() {{
            super.setBackground(Color.CYAN);
            this.getRootPane().setBackground(Color.BLUE);
            this.getLayeredPane().setBackground(Color.RED);
            this.getContentPane().setBackground(Color.YELLOW);
            this.setSize(400,340); 
            this.setVisible(true);
        }};
    }
}

But this example didn't help. And maybe the color is set by a superclass to Frame.

java.lang.Object
  java.awt.Component
      java.awt.Container
          java.awt.Window
              java.awt.Frame

My teory is that, since Swing paints itself, but uses a native Window, then is the native background painted before the resize, and the background of Swing is painted after the resize. But for native applications, the background is painted before the resize.

UPDATE: I tried with a Frame now, and it's not having the same problem. The background seam to be painted before the resize.

import java.awt.Color;
import java.awt.Frame;

public class B2Frame extends Frame {

    public static void main(String[] args) {
        new Frame() {{
            setBackground(Color.YELLOW);
            setSize(400,340);
            setVisible(true);
        }};
    }

}
Jonas
Thanks for the detailed answer, and trying all that stuff. I wonder why the difference, and if there's any way to fix it. I wonder if there's even any way to set the color of the JFrame itself (as opposed to getContentPane(), the only one that seems to do anything). I just tried getting the graphics object of the JFrame and setting its color, but that didn't work either. Any thoughts perhaps on where the default gray background is set? Maybe that has something to do with it.
dimo414
@dimo414: The default gray is set in JFrame: http://fuseyism.com/classpath/doc/javax/swing/JFrame-source.html with `setBackground(UIManager.getDefaults().getColor("control"));`
Jonas
@dimo414: You can get rid of this problem if you skip the native titlebar and border. See my question and answer at: http://stackoverflow.com/questions/2780780/how-to-add-support-for-resizing-when-using-an-undecorated-jframe/2781132#2781132
Jonas
@Jonas: True, I could do that, however that seems to me a very round-about solution, and, if you'll forgive me, a far more complicated one than I'm looking for. /Especially/ given that you found Frame does not have this same problem, I still wonder if there isn't some way to do the same in Swing, without resorting to abandoning Windows Aero entirely.
dimo414
+1  A: 

I also noticed this. For me this issue was solved with changing layout manager (I've used Free Form Layout before) and it worked pretty well (system color painting).

But eventually I switched back to FFL. Also some well known apps face this problem (f.e. SKYPE), but I actually don't mind it ...

Xorty
Do you have a link to "Free Form Layout"?
Jonas
It's default layout in NetBeans builder (which I made most of Swing apps in) ...
Xorty