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 7559439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:38:17+00:00 2026-05-30T12:38:17+00:00

OK, several questions about the test code below… I don’t have the full working

  • 0

OK, several questions about the test code below… I don’t have the full working program to post, but I’m hoping people know Swing well enough to take a stab at it. This is a JLabel inside of a JPanel(BoxLayout), and I’m working on the sizing of the label in the lower right.

What I get with the code as shown is a status box 300 width by 30 height. I have fiddled with the preferred size and the label minimum size, and it does not seem to behave in any rational way.

  1. Why does the JPanel Preferred Size affect the height but not the
    width? If I change the x dimension in setPreferredSize() to 0 or
    500, it still comes out 300 from the label.
  2. Why does the JLabel Minimum Size affect the width but not the
    height? If I comment the setPreferredSize() call and increase the
    label height to 30, nothing happens.
  3. I started out with JPanel setMinimumSize (commented), but it no longer has any
    effect – why does the JPanel require setPreferredSize()?
  4. If I change the label text from “” to ” “, this increases the height
    of the label. Since the label is not controlling the height here,
    why does this have any effect at all?

By the way, the createRigidArea() call is to force the separator to the right, rather than sticking to the left hand side of the screen. If there are any less kludgy ideas for this, I’d be grateful.

    private JComponent makeStatusBarTest() {
    JPanel statusPanel = new JPanel();
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.LINE_AXIS));
    statusPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
//  statusPanel.setMinimumSize(new Dimension(0, 30));
    statusPanel.setPreferredSize(new Dimension(500, 30));
    JLabel statusLabel = new JLabel();
    Border emptyBorder = BorderFactory.createEmptyBorder(5, 10, 5, 10);
    statusLabel.setBorder(emptyBorder);
    statusLabel.setText("");
    statusLabel.setMinimumSize(new Dimension(300, 20));
    statusPanel.add(statusLabel);
    statusPanel.add(new JSeparator(SwingConstants.VERTICAL));
    statusPanel.add(Box.createRigidArea(new Dimension(5000,0)));

    return statusPanel;
}
  • 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-30T12:38:19+00:00Added an answer on May 30, 2026 at 12:38 pm

    I can explain #1 and #2:

    From the BoxLayout javadocs: “BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout).”

    In other words, BoxLayout uses the internal components (in your case, statusLabel) to decide the widths, but the JPanel itself (within reason) to decide the heights.

    You can usually use Glue instead of RigidArea to move stuff around, but I agree that it takes some getting used to.

    #4 is Swing being too efficient – if the JLabel is empty the text rectangle is 0x0. Ultimately determined in SwingUtilities.layoutCompoundLabelImpl().

    I think #3 is because BoxLayout is trying to respect the preferred size of the internal components. Since setMinimumSize, arguably, overrides their preferred sizes.

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

Sidebar

Related Questions

I have seen several questions with people asking about the same problem but none
Several questions about functional programming languages have got me thinking about whether XSLT is
There have been several questions already posted with specific questions about dependency injection ,
There have been several questions recently about database indexing and clustered indexing and it
I've read several other questions about material in order to learn RoR. But my
There have been several questions posted to SO about floating-point representation. For example, the
I thought about migrating subversion a lot and I have several questions still open.
There have been several questions over the past few days about the proper use
I've seen several questions on SO about this, but none of them quite fit
There are several topics about what questions will be raised in an interview for

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.