banner

JInternal and JDesktopPane

Optional Reading

We will be using the Creating a GUI with JFC/Swing tutorial from Sun as a Reference source. You are not required to read the entire tutorial, I suggest you skim through it though to use a a supplement to what I give you here.

JInternalFrame and JDesktopPane

JInternalFrame and JDesktopPane are two classes that are always used together. A JDesktopPane is just another "Desktop" that lives within an application window. This lets you have windows inside of windows.

InternalFrames

The above screenshot shows a two versions of the JInternalFrame and an internal JOptionPane. You can run this and take a look at the code by downloading and opening the Netbeans project internalframes1.

To use an JInternalFrame, you first make a JInternalFrame, then add it to a JDesktopPane, and then show the frame by using the setVisible(true) method on the JInternalFrame (don't forget this step!).

JFrame jf = new JFrame()
JDesktopPane jdp = new JDesktopPane();
jf.add(jdp, BorderLayout.CENTER);
JInternalFrame jif = new JInternalFrame();
jdp.add(jif);
jif.setVisible(true);

Netbeans uses a form of this component, although the internal frames aren't as free-floating as with JInternalFrames in a JDesktopPane.

You can think of a JInternalFrame as just another type of JFrame, except you can exercise a little more control over what the user can and can't do with the window because the OS isn't controlling the window, Java is.

With JInternalFrames, you can control:

There are several versions of the constructor, but they are variations on default values of these traits.

JDesktopPane makes no effort to manage the location of iconified JInternalFrames. Once created on the Desktop, they don't move, so resizing is awkward and you may lose an icon if your window becomes too small.

It is easy to subclass JDesktopPane and put some quick location placement code. Basically, iconified frames are JComponent objects. You can't use the getAllFrames() from JDesktopPane because it doesn't return the icons, instead it returns the “invisible” JInternalFrames they represent. Instead use getComponent() on JDesktopPane and see when component is instanceof JDesktopPane.JDesktopIcon. Then you can manually relocate the icons.

Download and open internalframes2. This is a modified version of internalframes1 that uses a component listener to watch for changes in window sizes. When the window is resized, the icons are redrawn in the appropriate locations.

Icons before resizing:

Icon Management

Icons after resizing:

Icon management

="Icon management" width="383" height="227" />