Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing The Underlying Background Color Of A Swing Window

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 their informative answer below) that this is an issue with JFrames, but not AWT Frames - maybe that will help someone figure this out.

like image 573
dimo414 Avatar asked May 06 '10 06:05

dimo414


3 Answers

The frame is responsible for painting its background so you need to make sure you let it do its job.

You demonstrate this by setting:

System.setProperty("sun.awt.noerasebackground", "true");

This will cause the background to always be black on resize expansions. (So don't do it.)

The following worked for me:

  1. (AWT only) Set up double buffering using createBufferStrategy(2) - wrapped in addNotify() otherwise you'll run into exceptions during Frame creation

    (Step 1 is only necessary in AWT as Swing is double buffered by default.)

  2. Always (important) call super() in your Frame.paint() implementation

  3. Set the background colour with setBackground() then the background should always be the same colour when expanding the frame

Sample code:

    class InnerFrame extends Frame {
        public void addNotify() {
            super.addNotify();
            // Buffer
            createBufferStrategy(2);           
            strategy = getBufferStrategy();
        }

        public void paint(Graphics g) {
            super(g);
            //...
        }
        //...
     }
like image 32
Charles Goodwin Avatar answered Oct 16 '22 13:10

Charles Goodwin


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);
        }};
    }

}
like image 141
Jonas Avatar answered Oct 16 '22 13:10

Jonas


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

like image 1
Xorty Avatar answered Oct 16 '22 13:10

Xorty