Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
JLayeredPanes can hold InternalFrames. InternalFrames pop up a window-like component. Unlike a real window (Frame or Dialog), an InternalFrame cannot be dragged outside of the scope of its containing applet's panel.
To use an InternalFrame, ensure that the applet's View extends JLayeredPane.
InternalFrames do not normally have a layout manager. Instead the position and size of each component must be given before it will be displayed on the InternalFrames surface.
setLocation() setSize()
It is possible to specify whether an InternalFrame displays icons for iconifying (minimising), maximizing and closing.
setIconifiable(boolean b) setMaximizable(boolean b) setClosable(boolean b)
It is possible to programatically iconify or maximize an InternalFrame:
setIcon(boolean b) setMaximum(boolean b)
It is possible to specify what icon goes in the InternalFrame's title bar:
setFrameIcon(Icon icon)
To specify no icon, pass null to the setFrameIcon() method:
setFrameIcon(null)
If more than one InternalFrame is visible, then the layering of the various InternalFrames can be set:
moveToFront() moveToBack()
InternalFrameDemo example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InternalFrameDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } // note that View is a JLayeredPane, so that it can hold an Internal Frame public class View extends JLayeredPane implements MouseListener { private final JTextArea textArea = new JTextArea("Type some text here.\n\nClick right-mouse button to call inter frame"); private final MyInternalFrame internalFrame = new MyInternalFrame("An Internal Frame"); public View() { super(); // Because the container is a JDLayeredPane, // it should not have a LayoutManager. add(this.textArea); add(this.internalFrame); this.textArea.addMouseListener(this); } @Override public void paintComponent(Graphics g) { this.textArea.setLocation(0, 0); this.textArea.setSize(this.getWidth(), this.getHeight()); } @Override public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) // right mouse button { this.internalFrame.setVisible(true); } } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } class MyInternalFrame extends JInternalFrame implements ActionListener { private JButton button1 = new JButton("Button 1"); private JButton button2 = new JButton("Button 2"); private JLabel message = new JLabel(); public MyInternalFrame(String title) { super(title); // show the 'X' close icon and hide the internal frame on close this.setClosable(true); this.setDefaultCloseOperation(HIDE_ON_CLOSE); // do not show any icon this.setFrameIcon(null); this.setLayout(new GridLayout(1, 3)); this.setSize(300, 100); this.add(this.button1); this.add(this.message); this.add(this.button2); this.button1.addActionListener(this); this.button2.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); if (source == this.button1) { this.message.setText("Button 1 was hit."); } else if (source == this.button2) { this.message.setText("Button 2 was hit."); } } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.