by Dinesh Thakur Category: Swing

A layered pane is a Swing container which is used to hold the various components using the concept of layers. The components present in the upper layer overlaps the components present in the lower layer. The layered pane is created using the JLayeredPane class. the only constructor of this class is JLayeredPane ().

 

import java.awt.*;

import javax.swing.*;
public class JavaExampleLayeredPaneInJApplet extends JApplet
  {
      JLayeredPane LyrdPn=new JLayeredPane();
      JLabel Lbls[];
      public void init()
        {
           setContentPane(LyrdPn);
           LyrdPn.setLayout(null);
           Lbls = new JLabel[6];
           Lbls[0] = new JLabel("Content's Layer");
           Lbls[0].setOpaque(true);
           Lbls[0].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[0], LyrdPn.FRAME_CONTENT_LAYER.intValue());
           LyrdPn.add(Lbls[0]);
           Lbls[1] = new JLabel("By Default Layer");
           Lbls[1].setOpaque(true);
           Lbls[1].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[1], LyrdPn.DEFAULT_LAYER.intValue());
           LyrdPn.add(Lbls[1]);
           Lbls[2] = new JLabel("Paletter Layer");
           Lbls[2].setOpaque(true);
           Lbls[2].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[2],LyrdPn.PALETTE_LAYER.intValue());
           LyrdPn.add(Lbls[2]);
           Lbls[3] = new JLabel("Modal Layer");
           Lbls[3].setOpaque(true);
           Lbls[3].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[3], LyrdPn.MODAL_LAYER.intValue());
           LyrdPn.add(Lbls[3]);
           Lbls[4] = new JLabel("Popup Layer");
           Lbls[4].setOpaque(true);
           Lbls[4].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[4], LyrdPn.POPUP_LAYER.intValue());
           LyrdPn.add(Lbls[4]);
           Lbls[5] = new JLabel("Drag Layer");
           Lbls[5].setOpaque(true);
           Lbls[5].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[5], LyrdPn.DRAG_LAYER.intValue());
           LyrdPn.add(Lbls[5]);
           for(int loop_indx=0;loop_indx<6;loop_indx++)
                {
                    Lbls[loop_indx].setBounds(40 * loop_indx, 40 * loop_indx, 100, 60);       
                }
        }
   }
/*<APPLET CODE=JavaExampleLayeredPaneInJApplet.class WIDTH=360 HEIGHT=290></APPLET>*/

JLayeredPane in Java Swing Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles