Events happening when opening a new Window
Hello,
I'm opening a new window (implemented by a JFrame), and interested to know which events occur when the window is opened (either when calling pack method or when calling setVisible(true) on the frame). Especially, does a COMPONENT_RESIZED event occur when the window is opened?
Thanks in advance, Amit
[329 byte] By [
amit117a] at [2007-10-3 8:14:19]

Hi amit117,
try the following code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestListener {
private static class MyComponentListener implements ComponentListener {
public void componentResized(ComponentEvent e) {
Dimension dim = e.getComponent().getSize();
System.out.println("Component resized : ( " + dim.getWidth() + ", " + dim.getHeight() + " )" );
}
public void componentMoved(ComponentEvent e) {
Point pt = e.getComponent().getLocation();
System.out.println("Component moved : ( " + pt.getX() + ", " + pt.getY() + " )");
}
public void componentShown(ComponentEvent e) {
System.out.println("Component shows");
}
public void componentHidden(ComponentEvent e) {
System.out.println("Component hides");
}
}
private static JFrame frame;
public static void main(String[] args) {
frame = new JFrame("Child Frame");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocation(300,150);
frame.addComponentListener(new MyComponentListener());
JFrame mframe = new JFrame("Test Frame");
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mframe.setSize(600, 400);
mframe.setLocation(300,150);
mframe.getContentPane().setLayout(new FlowLayout());
JButton butShow = new JButton("Show");
butShow.addActionListener(new ShowHideAction());
mframe.getContentPane().add(butShow);
mframe.setVisible(true);
}
private static class ShowHideAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
((JButton)e.getSource()).setText(frame.isVisible()?"Show" : "Hide");
frame.setVisible(! frame.isVisible() );
}
}
}
You can apply the ComponentListener to any awt, swing component.