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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:22:32+00:00 2026-05-20T09:22:32+00:00

I have window containing multiple panels. I don’t have access to window code. (I

  • 0

I have window containing multiple panels. I don’t have access to window code. (I can modify only panel’s code.)

I removed few components from panel. Window has shrunk its size. But window is too small to display everything correctly.

I added line setPreferredSize(getPreferredSize());. Now window have right size.

What are side effects of setPreferredSize?

Edit: BorderLayout is used. Which should ignore getXXXSize(). My panel is in CENTRE. Panel which doesn’t fit the screen is on NORTH.

  • 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-20T09:22:32+00:00Added an answer on May 20, 2026 at 9:22 am

    This is what is happening:

    • getPreferredSize() looks whether the size was set before. If not, the method asks the LayoutManager of the component itself (which is your JPanel) about the preferred size, which is then calculated from the components.
    • setPreferredSize(...) then sets this value on the JPanel, memorizing it for later.
    • Later you remove some components of the JPanel.
    • Even later, when the window tries to re-layout itself (or is told to do so), the window’s (or contentpane’s/RootPane’s/…) Layoutmanager calls your JPanel’s getPreferredSize() method again.
    • now getPreferredSize() does not ask the JPanel’s LayoutManager, but simply returns the stored size previously set by setPreferredSize().

    For width, the BorderLayout is ignoring the preferred width of the NORTH and south component, it only takes CENTER, EAST and WEST into account. (Similarly for height).

    I just took a look at the implementation of BorderLayout.preferredLayoutSize (in 1.6.0_13 from Sun), and it works like this:

    The width is calculated as

    max(  EAST.width + CENTER.width + WEST.width + h-gaps,
          NORTH.width, SOUTH.width ) + insets
    

    The height is calculated as

    max( EAST.height, CENTER.height, WEST.height)
    + NORTH.height + SOUTH.height + v-gaps + insets
    

    (Each of the width/height are the values of the preferredSize of these components.)
    If some of the five components are missing, their height/width is not included, neither are the gaps.)

    It works the same for minimalLayoutSize, while maximumLayoutSize simply returns Integer.MAX_VALUE.

    So, in principle it should work out of the box.

    But in general, if the layout of the window is not under your control, you should not have to worry about components not under your control being cut off 🙂

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

Sidebar

Related Questions

I have a window containing a form (formPanel). Users can show this window clicking
Is there an osx mysql gui, which can run a script containing multiple statements,
I am using ExtJS to build a window containing several panels as items. One
I have an scope bar containing a NSSearchField. The bar can be shown and
I have a Windows Forms app, that has a single ElementHost containing a WPF
I have a Window where I have put a Frame. I would like to
I have a window that is set with NSBorderlessWindowMask, and also kCGDesktopWindowLevel. When a
I have a window (derived from JFrame) and I want to disable the close
I have a window with 2 column grid. Left column contains a browser control
I have a window in WPF which shows some media contents. This content contains

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.