javax.swing.InternalFrameListener - Internal Frame Listener Interface

This section provides a tutorial example on how to use javax.swing.JInternalFameListener interface that allows implementing classes to handle internal frame events.

The following program shows you how to create internal frame listeners:

/* JInternalFrameListenerTest.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
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:

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:

Internal Frame Listener
Internal Frame Listener

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

Creating Internal Frames inside the Main Frame

 javax.swing.JInternalFame - Internal Frame Class

 JInternalFameTest.java - Internal Frame Class Test

javax.swing.InternalFrameListener - Internal Frame Listener Interface

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Archived Tutorials

 References

 Full Version in PDF/EPUB