LayeredPane

JLayeredPanes can be used to layer multiple components on top of each other.

To use a LayeredPane, ensure that the applet's View extends JLayeredPane.

 

LayeredPanes 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()

LayeredPaneDemo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayeredPaneDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    // note that View extends from JLayeredPane
    public class View extends JLayeredPane implements MouseListener
    {
        private JComponent source;
        private int offsetX;
        private int offsetY;

        public View()
        {
            super();

            final JPanel redPanel = new JPanel();
            final JPanel greenPanel = new JPanel();
            final JPanel bluePanel = new JPanel();

            redPanel.setBackground(Color.red);
            greenPanel.setBackground(Color.green);
            bluePanel.setBackground(Color.blue);

            // add some components to the layeredPane
            this.add(redPanel);
            this.add(greenPanel);
            this.add(bluePanel);

            // a location and size must be set for each component that is
            // being added to a LayeredPane. Components will not be visible
            // until these two parameters are set
            redPanel.setLocation(0, 0);
            redPanel.setSize(100, 100);
            greenPanel.setLocation(50, 50);
            greenPanel.setSize(100, 100);
            bluePanel.setLocation(100, 100);
            bluePanel.setSize(100, 100);

            // add mouse listeners
            redPanel.addMouseListener(this);
            greenPanel.addMouseListener(this);
            bluePanel.addMouseListener(this);
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            this.source = (JComponent) e.getSource();
            this.offsetX = e.getX() - this.source.getX();
            this.offsetY = e.getY() - this.source.getY();

            if (e.getButton() == MouseEvent.BUTTON3)  // right mouse button
            {
                moveToFront(this.source);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
            this.source.setLocation(e.getX() - this.offsetX, e.getY() - this.offsetY);
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
        }
    }
}

Interactive Components

When interactive components, such as Buttons and TextFields, receive the mouse focus, they automatically display as the top-most layer. Therefore, LayerPanes should not be used to hold interactive components.

LayeredPane_VariousComponents_Demo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayeredPane_VariousComponents_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    // note that View extends from JLayeredPane
    public class View extends JLayeredPane implements MouseListener
    {
        private JComponent source;
        private int offsetX;
        private int offsetY;

        public View()
        {
            super();

            final JPanel panel = new JPanel();
            final JButton button = new JButton("Button");
            final JTextField textField = new JTextField("Textfield", 15);
            final JLabel label = new JLabel("Label");

            panel.add(new JLabel("Panel"));
            panel.setBackground(Color.red);

            // add some components to the layeredPane
            add(panel);
            add(button);
            add(textField);
            add(label);

            // a location and size must be set for each component that is
            // being added to a LayeredPane. Components will not be visible
            // until these two parameters are set
            panel.setLocation(0, 0);
            panel.setSize(100, 100);
            button.setLocation(150, 0);
            button.setSize((int) (button.getPreferredSize().getWidth()),
                           (int) (button.getPreferredSize().getHeight()));
            textField.setLocation(100, 100);
            textField.setSize(100, (int) (textField.getPreferredSize().getHeight()));
            label.setLocation(150, 150);
            label.setSize((int) (label.getPreferredSize().getWidth()),
                          (int) (label.getPreferredSize().getHeight()));

            // add mouse listeners
            panel.addMouseListener(this);
            button.addMouseListener(this);
            textField.addMouseListener(this);
            label.addMouseListener(this);
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            this.source = (JComponent) e.getSource();
            this.offsetX = e.getX() - this.source.getX();
            this.offsetY = e.getY() - this.source.getY();

            if (e.getButton() == MouseEvent.BUTTON3)  // right mouse button
            {
                moveToFront(this.source);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
            this.source.setLocation(e.getX() - this.offsetX, e.getY() - this.offsetY);
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>