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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:12:33+00:00 2026-05-21T20:12:33+00:00

When is the preferred size of a JPanel set if you don’t set it

  • 0

When is the preferred size of a JPanel set if you don’t set it explicitly?

I have the following code:

private static class ScrollableJPanel extends JPanel implements Scrollable {
    @Override
    public Dimension getPreferredScrollableViewportSize() {
                 return getPreferredSize();
    }
            .
            .
            .
}

and this code:

    myPanel = new ScrollableJPanel();
            // Add a bunch of controls to the panel.
    final JScrollPane scroll = new JScrollPane(myPanel);
    scroll.setAutoscrolls(true);
    scroll.setHorizontalScrollBarPolicy(
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

However, the preferred size of myPanel at the time getPreferredScrollableViewportSize() is being called seems to be an arbitrary value, (4225, 34), and prefSizeSet = false.

According to the Java documentation, this is a reasonable way to define this method. However, the actual size of myPanel is much higher than 34 pixels, so the view port is way too small.

What am I doing wrong here? Should I set the preferred size of myPanel explicitly? I tried that, but I want to use the actual size determined by the layout manager, not just make a guess that will be overridden later.

Note that my question isn’t how to set an exact size for the JPanel. I want the layout manager to do that. Nor do I want to have to explicitly set the preferred size. I thought that was also handled by the layout manager. What I want to know is why the layout manager is not setting a reasonable value for the preferred size itself so that I can return a reasonable PreferredScrollableViewportSize.

  • 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-21T20:12:34+00:00Added an answer on May 21, 2026 at 8:12 pm

    The Preferred size of a JPanel is based on the components in the panel. The Layout manager calculates a preferred size of the panel, based on the preferred size of the items in the panel, and if some of those things are panels, then it is a recursive calculation. If there are no sub components in the panel, then the preferred size will have to be set explicitly.

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

Sidebar

Related Questions

Code: import java.awt.Dimension; import javax.swing.*; public class Game extends JFrame { private static final
I have the following situation: in a JCombobox, the preferred size is based on
OK, several questions about the test code below... I don't have the full working
Is it possible to tell JPanel to set its size to fit all components
What is the preferred way to work with Singleton class in multithreaded environment? Suppose
What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites
Which is the preferred way of defining class properties in Python and why? Is
I have a subclass of JPanel , and I want it to, as soon
I have a JScrollpane that has a JPanel on the inside (and the panel
I want to specify that the size is 200dlu < preferred component size <

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.