This section provides a tutorial example on how to use javax.swing.JInternalFameListener interfacethat allows implementing classes to handle internal frame events.
The following program shows you how to create internal frame listeners:
/**
* JInternalFrameListenerTest.java
* Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JInternalFrameListenerTest
implements InternalFrameListener, ActionListener {
JFrame myFrame = null;
int count = 0;
public static void main(String[] a) {
(new JInternalFrameListenerTest()).test();
}
private void test() {
myFrame = new JFrame("Internal Frame Listener Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,300);
myFrame.setContentPane(new JDesktopPane());
JMenuBar myMenuBar = new JMenuBar();
JMenu myMenu = new JMenu("Frame");
JMenuItem myMenuItem = new JMenuItem("New");
myMenuItem.addActionListener(this);
myMenu.add(myMenuItem);
myMenuBar.add(myMenu);
myFrame.setJMenuBar(myMenuBar);
myFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
count++;
JInternalFrame f = new JInternalFrame("Frame "+count);
f.setResizable(true);
f.setClosable(true);
f.setMaximizable(true);
f.setIconifiable(true);
f.setSize(200,200);
f.setLocation(count*10,count*10);
f.addInternalFrameListener(this);
f.setVisible(true);
myFrame.getContentPane().add(f);
}
public void internalFrameActivated(InternalFrameEvent e) {
System.out.println("Internal frame activated");
}
public void internalFrameClosed(InternalFrameEvent e) {
System.out.println("Internal frame closed");
}
public void internalFrameClosing(InternalFrameEvent e) {
System.out.println("Internal frame closing");
}
public void internalFrameDeactivated(InternalFrameEvent e) {
System.out.println("Internal frame deactivated");
}
public void internalFrameDeiconified(InternalFrameEvent e) {
System.out.println("Internal frame deiconified");
}
public void internalFrameIconified(InternalFrameEvent e) {
System.out.println("Internal frame iconified");
}
public void internalFrameOpened(InternalFrameEvent e) {
System.out.println("Internal frame opened");
}
}
Run this program, you will see a blank frame with one menu called "Frame". If you:
Click "Frame" menu and "New" menu item to create first internal frame.
Click "Frame" menu and "New" menu item to create second internal frame.
Click the close icon on the first internal frame.
Click the maximize icon on the second internal frame.
Click the minimize icon on the second internal frame.
Click the minimized image of the second internal frame.
you will see the following messages generated from the internal frame listener methods.
Action performed - Clicked "New" menu item
Internal frame opened
Action performed - Clicked "New" menu item
Internal frame opened
Internal frame closing - Clicked the close icon
Internal frame closed
Internal frame activated - Clicked the maximize icon
Internal frame deactivated - Clicked the minimize icon
Internal frame iconified
Internal frame activated - Clicked the minimized image
Internal frame deiconified
At the end of the test, the main frame will look like this:
Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0 and 1.6.0.