Java Swing JLayeredPane not showing up
Asked Answered
S

2

5

I seem to be having some major issues with JLayeredPane. I have a BorderLayout() pane, and I'd like for the West-side element to contain a few JLayeredPane's on top of each other, so I can switch between them to show the right information.

The west pane should be 200 pixels wide and should be as long as the total window is. In my sample code I have added two layers to the JLayeredPanel, but they don't show up. They should be in the west pane.

Here is my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {
    private static JFrame mainFrame = new JFrame();
    private static JPanel mainPane = new JPanel();
    public Main(){}

    public static void initGui(){
        JLayeredPane westPanel = new JLayeredPane();
        westPanel.setPreferredSize(new Dimension(200,0));
        westPanel.setBackground(Color.blue);

        JPanel layerOne = new JPanel();
        layerOne.add(new JLabel("This is layer 1"));
        westPanel.add(layerOne, new Integer(0), 0);

        JPanel layerTwo = new JPanel();
        layerTwo.add(new JLabel("This si layer 2"));
        westPanel.add(layerTwo, new Integer(1), 0);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.yellow);

        JPanel eastPanel = new JPanel();
        eastPanel.setPreferredSize(new Dimension(200,0));
        eastPanel.setBackground(Color.red);

        mainPane = new JPanel(new BorderLayout());
        mainPane.add(westPanel, BorderLayout.WEST);
        mainPane.add(centerPanel, BorderLayout.CENTER);
        mainPane.add(eastPanel, BorderLayout.EAST);

        mainFrame = new JFrame("Learning to use JLayeredPane");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setBounds(200, 200, 800, 500);
        mainFrame.setContentPane(mainPane);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        initGui();
    }
}

What this results in: enter image description here

Saros answered 23/11, 2013 at 12:13 Comment(0)
L
12

JLayeredPane uses a null layout and so you are responsible for stating the size and location of all components added to it. If not they will default to a location of [0, 0] and a size of [0, 0].

Lauro answered 23/11, 2013 at 12:30 Comment(2)
Oh I see. Using setBounds() it does display. Is there any way to make it fill the whole west pane?Saros
@arbitter in simple calculation: is if your westpane's length is 10 you can add 5 inner pane of length 2, you just have to set your dimension properlyPhilia
P
4

try this, its working

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {
    private static JFrame mainFrame = new JFrame();
    private static JPanel mainPane = new JPanel();
    public Main(){}

    public static void initGui(){
        JLayeredPane westPanel = new JLayeredPane();
        westPanel.setLayout(null);
        westPanel.setPreferredSize(new Dimension(200,0));
        westPanel.setBackground(Color.blue);

        JPanel layerOne = new JPanel();
        layerOne.add(new JLabel("This is layer 1"));
        layerOne.setBounds(0, 0, 100, 100);
        westPanel.add(layerOne, new Integer(0), 0);

        JPanel layerTwo = new JPanel();
        layerTwo.add(new JLabel("This si layer 2"));
        layerTwo.setBounds(0, 100, 100, 100);
        westPanel.add(layerTwo, new Integer(1), 0);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.yellow);

        JPanel eastPanel = new JPanel();
        eastPanel.setPreferredSize(new Dimension(200,0));
        eastPanel.setBackground(Color.red);

        mainPane = new JPanel();
        mainPane.setLayout(new BorderLayout());
        mainPane.add(westPanel, BorderLayout.WEST);
        mainPane.add(centerPanel, BorderLayout.CENTER);
        mainPane.add(eastPanel, BorderLayout.EAST);

        mainFrame = new JFrame("Learning to use JLayeredPane");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setBounds(200, 200, 800, 500);
        mainFrame.setContentPane(mainPane);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        initGui();
    }
}
Philia answered 23/11, 2013 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.