Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Swing JInternalFrame

Part:   1   2 

This chapter describes:

  • JInternalFame class and related classes.
  • Creating internal frames.
  • Internal Frame Listeners.

JInternalFame and Related Classes

javax.swing.JInternalFrame - A Swing class representing a UI internal frame inside a regular frame. An internal frame supports almost all the features a regular frame does. Adding an internal frame to a regular frame should be done by adding the internal frame to a desktop pane first, then using the desktop pane as the content pane of the regular frame. Interesting methods of JInternalFrame include:

  • JInternalFrame(String) - Constructor to create an internal frame with the specified string as the frame title.
  • setResizable(boolean) - Method to set this frame to be resizable or not.
  • setClosable(boolean) - Method to set this frame to be closable or not.
  • setMaximizable(boolean) - Method to set this frame to be maximizable or not.
  • setIconifiable(boolean) - Method to set this frame to be iconifiable or not.
  • setSize(int, int) - Method to set the size of this frame. If not set, an internal frame will have zero size and not visible.
  • setVisible(boolean) - Method to set this frame to be visible or not. The default setting is not visible, you must call setVisible(true) to show an internal frame.
  • addInternalFrameListener(InternalFrameListener) - Method to add internal frame listeners to this internal frame.

javax.swing.JDesktopPane - A Swing class representing a container to host internal frames.

  • getSelectedFrame() - Method to return the selected internal frame.

javax.swing.event.InternalFrameListener - A Swing interface for the implementing class to handle internal frame events. Methods include:

  • internalFrameActivated(InternalFrameEvent) - Method to be called when the internal frame is activated.
  • internalFrameClosed(InternalFrameEvent) - Method to be called when the internal frame has been closed.
  • internalFrameClosing(InternalFrameEvent) - Method to be called when the internal frame is in the process of being closed.
  • internalFrameDeactivated(InternalFrameEvent) - Method to be called when the internal frame is de-activated.
  • internalFrameDeiconified(InternalFrameEvent) - Method to be called when the internal frame is de-iconified.
  • internalFrameIconified(InternalFrameEvent) - Method to be called when the internal frame is iconified.
  • internalFrameOpened(InternalFrameEvent) - Method to be called when the internal frame is opened.

Creating Internal Frames

The following program shows you how to create internal frames:

/**
 * JInternalFrameTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class JInternalFrameTest {
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("Internal Frames");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setSize(300,300);
      JDesktopPane myDesktop = new JDesktopPane();
      myFrame.setContentPane(myDesktop);
      JInternalFrame f = createFrame("Frame 1");
      f.setLocation(10,10);
      myDesktop.add(f);
      f = createFrame("Frame 2");
      f.setLocation(60,60);
      myDesktop.add(f);
      myFrame.setVisible(true);
   }
   private static JInternalFrame createFrame(String t) {
      JInternalFrame f = new JInternalFrame(t);
      f.setResizable(true);
      f.setClosable(true);
      f.setMaximizable(true);
      f.setIconifiable(true);
      f.setSize(200,200);
      f.setVisible(true);
      return f;
   }
}

Run this program and you should see two internal frames. You can resize, close, maximize, and minimize the internal frame.

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Swing JInternalFrame