adding JLayeredPane to JPanel
Asked Answered
F

2

11

I am trying to add a JLayeredPane to a JPanel and then add an image (JLabel icon) and a button to the JLayeredPane, but neither show up. I've tested the image without the button and the layeredpane so I know that works. Here is some of the code I am using. Is there something I am missing or doing wrong?


public class MyClass extends JPanel 
{
    private JLayeredPane layeredPane;
    private JLabel imageContainer = new JLabel();
    private JButton info = new JButton("i");

    MyClass(ImageIcon image)
    {
        super();

        this.imageContainer.setIcon(image);

        this.layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(300, 300));
        layeredPane.add(imageContainer, new Integer(50));
        layeredPane.add(info, new Integer(100));

        this.add(layeredPane);
    }
}       

Fungosity answered 19/11, 2009 at 15:31 Comment(1)
Without seeing where and how you add the MyClass to a frame of some sort, there's no telling where you've gone wrong.Quassia
S
17

From the tutorial

By default a layered pane has no layout manager. This means that you typically have to write the code that positions and sizes the components you put in a layered pane.

See the changes to your code:

import java.awt.*;
import javax.swing.*;
public class MyClass extends JPanel {
    private JLayeredPane layeredPane;
    private JLabel imageContainer = new JLabel();
    private JButton info = new JButton("i");

    MyClass(ImageIcon image) {
        super();

        this.imageContainer.setIcon(image);

        this.layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(300, 300));
        layeredPane.add(imageContainer, new Integer(50));
        layeredPane.add(info, new Integer(100));
        this.add(layeredPane);
        // CHANGED CODE
        // Manually set layout the components. 
        imageContainer.setBounds( 0, 0,  
                                  image.getIconWidth(),
                                  image.getIconHeight() ); 
        info.setBounds( 200, 00,  50, 40 );
    }
    public static void main( String [] args ) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new MyClass( new ImageIcon("logo.png")  ) );
        frame.pack();
        frame.setVisible( true );
    }
}       

Additional notes:

1) It is better ( in my opinion ) to put the opening brace in the same line. That's how most Java code looks like.

2) Avoid inheriting from JPanel ( or any other component ) if you don't are not really creating a subclass. You can use it directly without having to inherit ( unless you're indeed creating a new component.

Sparrowgrass answered 19/11, 2009 at 15:45 Comment(4)
Thanks for the answer! In response to the opening brace remark, the reason for ME not doing it that way is due to white space readability. I feel this is a visual design concern since it has no proformance impact. It helps me and I know it helps others(not all) who are not familiar to the code.Fungosity
@asawilliams: Indeed it has no other impact but code readability. If you were already aware of that style and you're avoiding it intentionally I think it is ok. I do the recommendation anyway for those who don't know it. I've found developers coming from others programming languages resists to adopt this style. By any chance did you program in other curly braces programming language before? As for the help, you're welcome ;)Sparrowgrass
yeah, C, C++, C#, Actionscript 3, php, ... . It was just the way I was taught. I can understand how some programmers like to have tight code, but I don't see any other reason to do it other than preference.Fungosity
It is beginning to look like the JLayeredPane is 'outside' the normal Layout and Panel framework for Swing/AWT -- This is not an isolated post on StackOverflow or the wider-world-web ... The most modest requirement on a JLayeredPane appears to be at least that it fits 100% like a "fill" constraint.Gratiana
R
4

JLayeredPane has a null layout manager by default, so in your example you'll need to set the location and size of the child components. You can set a layout manager on the JLayeredPane, but that will most likely negate the layered rendering I'm guessing you want, since you're using a layered pane.

Rainbow answered 19/11, 2009 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.