JFrame flicker on programmatic resize.
Here is the code:
package ui.components;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
publicclass Test{
public Test()
{
final JFrame frame =new JFrame();
frame.setBounds(100, 100, 300, 300);
frame.setBackground(Color.blue);
JPanel panel =new JPanel(new BorderLayout());
JPanel up =new JPanel(new BorderLayout());
up.setPreferredSize(new Dimension(100, 100));
up.setBackground(Color.blue);
JButton button =new JButton("add size/shrink size");
button.addActionListener(
new ActionListener(){
boolean addf =true;
publicvoid actionPerformed(ActionEvent e)
{
if (addf){
frame.setSize(
frame.getWidth(),
frame.getHeight() + 50
);
}else{
frame.setSize(
frame.getWidth(),
frame.getHeight() - 50
);
}
addf = !addf;
}
}
);
up.add(button, BorderLayout.CENTER);
JPanel bp =new JPanel();
bp.setBackground(Color.red);
bp.setPreferredSize(new Dimension(100, 200));
panel.add(up, BorderLayout.CENTER);
panel.add(bp, BorderLayout.SOUTH);
frame.setContentPane(panel);
SwingUtilities.invokeLater(
new Runnable(){
publicvoid run()
{
frame.setVisible(true);
}
}
);
}
publicstaticvoid main(String[] args)
{
new Test();
}
}
Why the frame flickers when the button is pressed ?
I see that JFrame is filled with it's background color.
I know that "sun.awt.noerasebackground" property can be changed to fix that, but I don't want to change any global settings as the effect applies only to one frame.
Thank you.

