Demonstrating the ComponentListener
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class ComponentTest {
public static void main(String args[]) {
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
ComponentListener comp = new ComponentListener() {
public void componentHidden(ComponentEvent e) {
dump("Hidden", e);
}
public void componentMoved(ComponentEvent e) {
dump("Moved", e);
}
public void componentResized(ComponentEvent e) {
dump("Resized", e);
}
public void componentShown(ComponentEvent e) {
dump("Shown", e);
}
private void dump(String type, ComponentEvent e) {
System.out.println(e.getComponent().getName() + " : " + type);
}
};
JButton left = new JButton("Left");
left.setName("Left");
left.addComponentListener(comp);
final JButton right = new JButton("Right");
right.setName("Right");
right.addComponentListener(comp);
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
right.setVisible(!right.isVisible());
}
};
left.addActionListener(action);
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
left, right);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.show();
}
}
Related examples in the same category