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

  • SEARCH
  • Home
  • 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 6125711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:16:45+00:00 2026-05-23T16:16:45+00:00

I think I solved my problem, but I don’t know why it works this

  • 0

I think I solved my problem, but I don’t know why it works this way, so I’m hoping someone can explain it to me so I don’t do the same mistake again in the future.

Here’s a quick example that is compilable of what I’m trying to do:

public class BoxLayoutTest extends JFrame
{
  public BoxLayoutTest()
  {
    setSize(400,300);
    JPanel mainPanel = new JPanel(new FlowLayout());
    setContentPane(mainPanel);

    JPanel subPanel = new JPanel();
    subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.PAGE_AXIS));
    subPanel.setBackground(Color.BLUE);

    JLabel labelTest = new JLabel("This is a test");
    subPanel.add(labelTest);

    labelTest.setPreferredSize(new Dimension(150, 20));
    mainPanel.add(subPanel);
    System.out.println(mainPanel.getSize());
  }

  public static void main( String[] args )
  {
    BoxLayoutTest testFrame = new BoxLayoutTest();
    testFrame.setVisible(true);
  }
}

At first, I had problems with the panel containing the JLabel not resizing like it should with the preferred size. I found out that it was because I was using some variation of mainPanel.getSize() as a preferred size for my subpanels. In this example, I’m using actual number values, which work.

The reason why it didn’t work the old way (and that’s actually the thing I’d like someone to explain), is why, as seen in the SOP line, mainPanel.getSize() returns a width and a height of 0 while it clearly takes the whole screen, which is 400×300.

Thanks @camickr for telling me I shouldn’t set a preferredSize for my Panels, this helped me figure out where the problem was coming from.

  • 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-05-23T16:16:46+00:00Added an answer on May 23, 2026 at 4:16 pm

    Why [does] mainPanel.getSize() returns a width and a height of 0?

    Until pack() “causes this Window to be sized to fit the preferred size and layouts of its subcomponents,” the dimensions will be zero.

    System.out.println(mainPanel.getSize());
    this.pack();
    System.out.println(mainPanel.getSize());
    

    Console:

    java.awt.Dimension[width=0,height=0]
    java.awt.Dimension[width=160,height=30]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing what I think is a simple problem with Hibernate, but can't solve
i have a problem that i can't solve ! (sqlite3, but i think it
I can think of a few messy ways to solve this, but it strikes
you will think: "This problem has been solved many, many times. So why doesn't
I could solve this using loops, but I am trying think in vectors so
I think most people know how to do this via the GUI (right click
I think this is specific to IE 6.0 but... In JavaScript I add a
I know there has been a lot of posts on this but it still
I think I have a problem with multiple clicks on the same button. I
I read this topic, but his problem maybe different from mine Writing to both

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.