How Do I get Notified when the JFrame Gets Resized?

K

KevinSimonson

What do I have to do to be notified when the JFrame my JPanel is in
gets resized? Like if somebody clicks on the corner and drags it so
that I have a new width and new height, or if somebody clicks on the
button that makes the JFrame take up the whole screen?

Kevin S
 
A

Arne Vajhøj

What do I have to do to be notified when the JFrame my JPanel is in
gets resized? Like if somebody clicks on the corner and drags it so
that I have a new width and new height, or if somebody clicks on the
button that makes the JFrame take up the whole screen?

Try look at this code:

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class ResizeCatcher extends JFrame implements ComponentListener {
public ResizeCatcher() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
addComponentListener(this);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new ResizeCatcher();
f.setVisible(true);
}
});
}
public void componentResized(ComponentEvent e) {
if(!e.paramString().equals("COMPONENT_RESIZED (0,0 200x200)")) {
JOptionPane.showMessageDialog(this, "My size is perfect
thank you");
setSize(200, 200);
}
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
}

Arne
 
R

Roedy Green

What do I have to do to be notified when the JFrame my JPanel is in
gets resized? Like if somebody clicks on the corner and drags it so
that I have a new width and new height, or if somebody clicks on the
button that makes the JFrame take up the whole screen?

A JFrame is both a Window and a Container. Check the inherited
methods of the form addXXXXListener.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top