Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8095537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:09:51+00:00 2026-06-05T21:09:51+00:00

I was trying out JLayeredPane. So, in the following code, I created a JLayeredPane

  • 0

I was trying out JLayeredPane. So, in the following code, I created a JLayeredPane and a JLabel. I added the label to the layered pane, which I added to a JPanel. This panel was then added to a JFrame.

public static void main(String[] args) {
        frame = new JFrame("LayeredPane Example");
        frame.setPreferredSize(new Dimension(500,500));
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(400, 400));
        JLabel label = new JLabel("Label on LayeredPane");
        label.setLocation(200, 200);
        System.out.println("Width " + label.getWidth() );
        label.setBounds(20, 20, 400, 40);
        layeredPane.add(label);
        layeredPane.setLayer(label, 10, 1);
        frame.setLayout(new BorderLayout());        
        JPanel panel = new JPanel();
        panel.add(layeredPane);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

Now the problem is that if I do not have the statement label.setBounds(20, 20, 400, 40);, then the label does not appear on the layered pane. This raises two questions:

  1. Why is setBounds so important?
  2. Probably a part of my previous questions answer, the label had an initial height and width of 0 before setting bounds, which might be the reason setBounds is important. In that case, I want to know how can I determine appropriate bounds for a Swing component when I am adding it to a JLayeredPane. (If my bounds are less than the appropriate size of the component, the component will appear hidden)

Edit:
The first question was answered earlier in more detail here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:09:53+00:00Added an answer on June 5, 2026 at 9:09 pm

    Regarding:

    1. Why is setBounds so important: A JLayeredPane uses essentially a null layout, and whenever you use null layouts, you the coder are completely responsible for both the size and position of the components that you add. That’s simply the rules of this layout.
    2. How to determine the appropriate bounds: One thing I’ve done is simply to let the component tell me what its preferredSize is and then use it for its size:

    myJLabel.setSize(myJLabel.getPreferredSize());

    Another thing I’ve done is to use non-opaque JPanels for each layer of my JLayeredPane, give these JPanels appropriate layouts and then add my components to the appropriate layer JPanel. I then use a listener to be sure that the layer JPanel’s size matches that of its JLayeredPane container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying out some code on Ubuntu. I'm trying to run the following
I am trying out WebIssues link text which allows for a client-server issue tracker.
been trying out this for quite some time but I'm still unable to built
Trying out Linq for NHibernate and first noticed this: var list1 = (from ut
Trying out the following, will result in error. How do I retrieve the values
I trying out using regEx to split a String which gives me an array
Am trying out structuremap for the first time and am getting the following compiler
I have been trying out EF 4.1 ( code first ) with MVC 3.
I'm trying out very simple search popup on the JqGrid. Please see the code
I trying out some problems in Google's Code Jam. For the question Store Credit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.