Home Java Example Java Swing Java JLayeredPane example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java JLayeredPane example

Advertisement
In this section we will discuss about how to overlap the JPanels.

Java JLayeredPane example

In this section we will discuss about how to overlap the JPanels.

javax.swing.JLayeredPane provides the feature to overlap the components on each other. Depth of components in the container is specified by an Integer object. Panel associated with the higher integer value is situated on the top.

Example

Here I am giving a simple example of overlapping of three panels. These panels will be overlapped after clicking on a button showed on form. For this I have created a class named JavaJLayeredPane.java where I have created a JFrame object, JLayeredPane object, JButton object. as well as created four different JPanel's object. Then set their bounds. Each panels are of different sizes and positioned at different axes. Now added a button on an another JPanel as well as add an action listener to this button that when the button will be clicked the overlapped panels will be seen on the frame. To create the GUI's I have created a method createUI() and to perform the action on button I have implemented the ActionListener and its method actionPerformed(). Then in the main() method created an object of JavaJLayeredPane class and called the createUI() method.

Source Code

JavaJLayeredPane.java

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

public class JavaJLayeredPane implements ActionListener{
    JFrame frame;
    JLayeredPane layeredPane;
    JPanel bigPanel;
    JPanel mediumPanel;
    JPanel smallPanel;
	JPanel panel;
	JButton button = new JButton("Click Here");
    public void createUI()
    {
		frame = new JFrame("JPanel alignment example");
        frame.setPreferredSize(new Dimension(600, 400));
        frame.setLayout(new BorderLayout());

		layeredPane = new JLayeredPane();
        frame.add(layeredPane, BorderLayout.CENTER);
		
        layeredPane.setBounds(0, 0, 600, 400);

		bigPanel = new JPanel();
        bigPanel.setBackground(Color.BLUE);
        bigPanel.setBounds(50, 10, 150, 150);
        
		mediumPanel = new JPanel();
        mediumPanel.setBackground(Color.GREEN);
        mediumPanel.setBounds(50, 175, 100, 100);        

		smallPanel = new JPanel();
		smallPanel.setBackground(Color.RED);
        smallPanel.setBounds(50, 300, 50, 50);        

		layeredPane.add(bigPanel, new Integer(0), 0);
        layeredPane.add(mediumPanel, new Integer(1), 0);
		layeredPane.add(smallPanel, new Integer(2), 0);

		button.addActionListener(this);

		panel = new JPanel();
        panel.add(button);		
		frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
        frame.setVisible(true);
    }// createUI()

	public void actionPerformed(ActionEvent ae)
	{
		button = (JButton)ae.getSource();
		button.setVisible(false);

		layeredPane.setBounds(0, 0, 600, 400);

		bigPanel.setBounds(200, 100, 150, 150);
        bigPanel.setOpaque(true);

		mediumPanel.setBounds(225, 125, 100, 100);
        mediumPanel.setOpaque(true);

		smallPanel.setBounds(250, 150, 50, 50);
        smallPanel.setOpaque(true);

		layeredPane.add(bigPanel, new Integer(0), 0);
        layeredPane.add(mediumPanel, new Integer(1), 0);
		layeredPane.add(smallPanel, new Integer(2), 0);
	}// actionPerformed()

    public static void main(String[] args) {
        JavaJLayeredPane overlapPane = new JavaJLayeredPane();
		overlapPane.createUI();
    }// main closed
}// class closed

Output

When you will execute the above Java code you will get the output as follows :

When you will click on button (click here) then the output will be as follows :

Download Source Code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 11, 2013

Ask Questions?    Discuss: Java JLayeredPane example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com