1

I have a JLayeredPane. My program works something like this:

JPanel p1 = new JPanel(new BoxLayout(p1, BoxLayout.X_AXIS));
JPanel p2 = new JPanel(new BoxLayout(p2, BoxLayout.Y_AXIS));
JLayeredPane lp = new JLayeredPane();
lp.add(p1, 1);
lp.add(p2, 0);

Both p1 and p2 have components like buttons, etc...

The issue is that when I add both JPanels to the JLayeredPane, NOTHING appears.

I tried changing the layout of the JLayeredPane().

For example, I did:

lp.setLayout(new BoxLayout(lp, BoxLayout.X_AXIS));

Then, the JPanels do show, but they are shown adjacent, not respecting the layers of the JLayeredPane.

Am I forced to use a null layout?

How can I make my JLayeredPane respect the layers and show my two BoxLayout JPanels correctly?

When I give my JLayeredPane a layout, it shows the panels, but it is not respecting the layers at all.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ThePrince
  • 818
  • 2
  • 12
  • 26
  • 2
    **NEVER** use a `null` layout. – syb0rg Apr 15 '13 at 22:51
  • Is this the problem: http://docs.oracle.com/javase/tutorial/uiswing/components/problems.html#layeredpane -- The 2nd argument to `add()` has to be an `Integer` and not an `int` as the `int` version of the method is different. – Lee Meador Apr 15 '13 at 22:51
  • When I don't establish a layout, NOTHING appears. When I add any layout, it just behaves as if it wasn't a layeredpane, and adds components adjacent to one another visually, rather than one on top of the other. – ThePrince Apr 15 '13 at 22:56
  • THank you for the Integer issue. However, when I use Integer, it still acts like a regular layout. It adds the panels adjacent to each other even if they are supposed to be in different layers. – ThePrince Apr 15 '13 at 22:56
  • The JLayeredPane acts as if it were using null layout. You must specify your JPanel's size and position when adding it to the JLayeredPane. – Hovercraft Full Of Eels Apr 15 '13 at 23:02

3 Answers3

6

You need a layout manager which understands the Z-Axis. The default layout managers don't understand the Z-Axis of the JLayeredPane.

If you simply want to overlay stuff on top of each other you can use a LayoutManager like this:

JLayeredPane layeredFooPane = new JLayeredPane();
// The magic!
layeredFooPane.setLayout(new LayeredPaneLayout(layeredPane));
// Add components:
layeredFooPane.add(fooComponent, new Integer(JLayeredPane.DEFAULT_LAYER + 10));
layeredFooPane.add(barComponent, JLayeredPane.DEFAULT_LAYER);

LayoutManager Class:

public class LayeredPaneLayout implements LayoutManager {

    private final Container target;
    private static final Dimension preferredSize = new Dimension(500, 500);

    public LayeredPaneLayout(final Container target) {
            this.target = target;
    }

    @Override
    public void addLayoutComponent(final String name, final Component comp) {
    }

    @Override
    public void layoutContainer(final Container container) {
            for (final Component component : container.getComponents()) {
                    component.setBounds(new Rectangle(0, 0, target.getWidth(), target.getHeight()));
            }
    }

    @Override
    public Dimension minimumLayoutSize(final Container parent) {
            return preferredLayoutSize(parent);
    }

    @Override
    public Dimension preferredLayoutSize(final Container parent) {
            return preferredSize;
    }

    @Override
    public void removeLayoutComponent(final Component comp) {
    }
}
Jasper Siepkes
  • 1,412
  • 16
  • 25
  • This works well - though the only issue I run into is when I have components on each of these layers, some of the components from the underneath layers try to pop out trough to the top (when I hover the cursor over them). I can eliminate this behavior by simply setting all of the components of the layer underneath to disabled ( setEnabled(false) ), because after all, they have no business being enabled if they are currently hidden - this just adds one extra bit of overhead. – Michael Plautz Oct 14 '13 at 20:46
4

The JLayeredPane acts as if it were using null layout. You must specify your JPanel's size and position when adding it to the JLayeredPane. One of the few times I'll recommend this:

JPanel p1 = new JPanel(new BoxLayout(p1, BoxLayout.X_AXIS));
JPanel p2 = new JPanel(new BoxLayout(p2, BoxLayout.Y_AXIS));
JLayeredPane lp = new JLayeredPane();
lp.add(p1, 1);
lp.add(p2, 0);

p1.setSize(lp.getPreferredSize());
p2.setSize(lp.getPreferredSize());
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

I was having a similar problem. Instead of passing the layer when you add the panes, try to do it separately:

lp.setLayer(p1, 1);
lp.setLayer(p2, 0);
lp.add(p1);
lp.add(p2);

Layers must be defined before adding components. You can also try to use JLayeredPane.DEFAULT_LAYER instead of 0.

In my case, my JLayeredPane had GridBagLayout, I don't know if it works with default layout.