JFrame flickers when being dragged

There may have already some posts on this topic but I just could not find a solution to it. How can I prevent the flickers when a Jrame is dragged ?
[155 byte] By [jdevp2a] at [2007-10-3 3:03:45]
# 1
Umm I dont think ive ever seen this problem, can you post some code?
k2snowman69@yahoo.coma at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for the response. I found out that the flickers are actually caused by the JDialog containers used as control panels in my program. The reason for using JDialog instead of JToolBar is that I could not make JToolBar starting with float. Here is a sample code. You can see that the frame flickers when you drag it around with the JDialog containers on top of it.

import javax.swing.*;

import java.awt.*;

public class TestFlicker extends JFrame

{

public TestFlicker()

{

JDialog jdlCtrl[]=new JDialog[3];

JPanel jpl=new JPanel();

jpl.setPreferredSize(new Dimension(600,400));

jpl.setBackground(Color.blue);

setSize(600,400);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(jpl, BorderLayout.CENTER);

pack();

setVisible(true);

for(int i=0; i<3; i++)

{

jdlCtrl=new JDialog(this);

jdlCtrl.setSize(100,100);

jdlCtrl.setVisible(true);

}

jdlCtrl[0].setLocation(100,20);

jdlCtrl[1].setLocation(100,140);

jdlCtrl[2].setLocation(100,300);

}

public static void main(String args[])

{

new TestFlicker();

}

}

jdevp2a at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 3
Flickering happens when it takes too long to repaint. You might be able to overwrite a method so that when it's dragged, instead of repainting all the components individually, it saves an image from the last time it was repainted and just draws that.
myjavasticka at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 4

Use the "Code" formatting tags when posting code so the code is readable and compileable.

This seems to be a "feature" of Swing. It even happens in the [url http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#dialogdemo]Dialog Demo[/url] from the Swing tutorial. Its more noticable in your example because you've changed the background to something other than the default gray.

It seems to work a little better when you add this property at the start of your program:

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

camickra at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks for response. I did set the system properity. It works better when drag the Jfram but it does not work well when you resize the window. By the way, I did use the the code tag when copy and paste the code. Do not know why it does not format the code correctly.
jdevp2a at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 6

> By the way, I did use the the code tag when copy and paste the code.

> Do not know why it does not format the code correctly.

No you didn't. Either the code would be formatted correctly, or the tags would appear in the posting.

Thats why the forum has a "Preview" button. So you can check you message before posting it.

camickra at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 7
Glad to see that this problem is fixed in 1.6.0. I tried it with 1.6. The flickers issue is gone.
jdevp2a at 2007-7-14 20:53:42 > top of Java-index,Desktop,Core GUI APIs...