I'm trying to resize the JFrame on button click, the code works well (but i don't know if this is the best way to achieve this).
But the problem is: While resizing, the JFrame is slowly revalidated. The GIF can explain what exactly is happening:
The piece of code is:
chatButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread (new Runnable() {
public void run(){
int width = frame.getWidth();
int height = frame.getHeight();
int buttonWidth = chatButton.getWidth();
if (frame.getWidth() < 1150) {
while (frame.getWidth() < 1150) {
width = frame.getWidth();
frame.setSize(width + 2 , height);
chatButton.setLocation(width - buttonWidth , 0);
frame.invalidate();
frame.validate();
}
} else {
while (frame.getWidth() > 897) {
width = frame.getWidth();
frame.setSize(width - 2 , height);
chatButton.setLocation(width - buttonWidth , 0);
frame.invalidate();
frame.validate();
}
}
}
}).start();
}
});
I've put it in a Runnable because it wasn't revalidating until the resize is over.
I've also tried repaint()
and revalidate()
but they didn't solve the problem at all.
What can I do?
Thanks in advance.