29.10. (Optional) Creating Internal Frames |
You can create multiple windows , as discussed in §15.12, "Creating Multiple Windows ." Java also allows you to use the JInternalFrame class to create windows within a window. This user interface is commonly known as a multiple document interface or MDI . It was once quite popular and was used in the earlier versions of many popular Windows software programs. Now, however, MDI is rarely used. That is why this section is marked optional.
The JInternalFrame class is almost the same as the external JFrame class. The components are added to the internal frame in the same way as they are added to the external frame. An internal frame can have menus , title, Close icon, Minimize icon, and Maximize icon just like an external frame. The following are the major differences:
JInternalFrame extends JComponent , and JFrame extends the AWT Frame class. Therefore, JInternalFrame is a Swing lightweight component, and JFrame is a Swing heavyweight component.
Both JInternalFrame and JFrame are used to hold other components. JFrame is a top-level window component, and JInternalFrame must be contained inside a JDesktopPane of a JFrame or a JApplet .
Here are the steps to create an internal frame inside another window:
1. | Use a JFrame or a JApplet as the outer window. |
2. | Create a JDesktopPane and add it to the content pane of a JFrame or JApplet . Usually, the JDesktopPane is added to the center of the content pane. |
3. | Create a JInternalFrame and add it to the JDesktopPane using the add method. |
4. | Use the setVisible(true) method to display the internal frame. |
Listing 29.9 gives an example that creates internal frames to display flags in an applet. You can select flags from the Flags menu. Clicking a menu item causes a flag to be displayed in an internal frame, as shown in Figure 29.23.
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class ShowInternalFrame extends JApplet { 6 // Create image icons 7 private ImageIcon USIcon = 8 new ImageIcon(getClass().getResource( "image/usIcon.gif" )); 9 private ImageIcon CanadaIcon = 10 new ImageIcon(getClass().getResource( "image/caIcon.gif" )); 11 12 private JMenuBar jMenuBar1 = new JMenuBar(); 13 private JMenuItem jmiUS = new JMenuItem( "US" ); 14 private JMenuItem jmiCanada = new JMenuItem( "Canada" ); 15 private JLabel jlblImage = new JLabel(USIcon, JLabel.CENTER); 16 17 // Create JDesktopPane to hold the internal frame 18 private JDesktopPane desktop = new JDesktopPane(); 19 private JInternalFrame internalFrame = 20 new JInternalFrame( "US" , true , true , true , true ); 21 22 public ShowInternalFrame() { 23 desktop.add(internalFrame); 24 25 this .setSize( new Dimension( 400 , 300 )); 26 this .getContentPane().add(desktop, BorderLayout.CENTER); 27 28 jlblImage.setIcon(USIcon); 29 internalFrame.setFrameIcon(USIcon); 30 31 internalFrame.add(jlblImage); 32 internalFrame.setLocation( 20 , 20 ); 33 internalFrame.setSize( 100 , 100 ); 34 internalFrame.setVisible( true ); 35 36 JMenu jMenu1 = new JMenu( "Flags" ); 37 jMenuBar1.add(jMenu1); 38 jMenu1.add(jmiUS); 39 jMenu1.add(jmiCanada); 40 41 this .setJMenuBar(jMenuBar1); 42 43 jmiUS.addActionListener( new ActionListener() { 44 public void actionPerformed(ActionEvent e) { 45 jlblImage.setIcon(USIcon); 46 internalFrame.setFrameIcon(USIcon); 47 internalFrame.setTitle( "US" ); 48 } 49 }); 50 51 jmiCanada.addActionListener( new ActionListener() { 52 public void actionPerformed(ActionEvent e) { 53 jlblImage.setIcon(CanadaIcon); 54 internalFrame.setFrameIcon(CanadaIcon); 55 internalFrame.setTitle( "Canada" ); 56 } 57 }); 58 } 59 } |
An image icon is displayed on a label (line 15). The label is placed inside an internal frame (line 31). As shown in Figure 29.23, an internal frame looks like an external frame. Internal frames can be used much the same way as external frames, except that internal frames are always placed inside a JDesktopPane . JDesktopPane is a subclass of JLayeredPane . Since JDesktopPane is also a subclass of JComponent , it can be placed into the content pane of a JFrame or a JApplet .
The properties of JInternalFrame and JFrame are very similar. You can set a title, an internal frame icon, size , and visible for an internal frame. You may modify this example to add menus to the internal frame too.