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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:17:03+00:00 2026-06-14T00:17:03+00:00

I have a panel layout issue that I can’t seem to solve. I have

  • 0

I have a panel layout issue that I can’t seem to solve. I have the following code:

public class Test extends ApplicationFrame {
    public Test(String title) {
        super(title);

        JPanel jpanel = new JPanel();
        jpanel.setPreferredSize(new Dimension(100 * 2, 300));
        jpanel.setBackground(new Color(0xFF0000));

        JScrollPane scrollPane = new JScrollPane(jpanel);
        scrollPane.setBackground(new Color(0xFF0000));
        scrollPane.getViewport().setPreferredSize(new Dimension(100 * 2, 300));

        this.add(scrollPane, BorderLayout.WEST);
        this.setPreferredSize(new Dimension(500, 500));
    }

    public static void main(String args[]) {
        Test test = new Test("Layout Test");
        test.pack();
        RefineryUtilities.centerFrameOnScreen(test);
        test.setVisible(true);
    }
}

to create the following layout:

enter image description here

When I drag the right hand side of the window and move it over into the red JPanel and JScrollPane, I’d like the horizontal scroll bars to appear on the JScrollPane. But currently the window just shrinks without showing the horizontal scroll bar. Is this possible?

I’d like to keep the BorderLayout.WEST since my use case for this is to keep a JFreeChart from stretching when I don’t have a big enough chart to fill the entire window.

  • 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-14T00:17:04+00:00Added an answer on June 14, 2026 at 12:17 am

    There are many reasons not to use setPreferredSize(), but the critical one here is that the desired result depends on the preferred size of the enclosed components. In particular,

    • ChartPanel has the default FlowLayout specified by JPanel, but the chart ignores this and adjusts to fill the space available; you can override getPreferredSize() to choose any desired initial geometry.

    • The scroll bars of a JScrollPane only appear when the size of the viewport is smaller than the size of the content. BorderLayout.WEST and BorderLayout.EAST don’t change, so the scroll bars never appear.

    Invoking pack() “causes this Window to be sized to fit the preferred size and layouts of its subcomponents.” In the example below, the frame’s size is artificially reduced after pack() in order to cause the scroll bars to appear. GridLayout causes the two panels to be equal is size. Resize the frame to see the effect.

    Addendum: If you want a ChartPanel to have a fixed size, add it to a JPanel; the default FlowLayout uses the preferred size.

    image

    public class ChartPanelTest {
    
        private static final int N = 64;
        private static final int SIZE = 400;
        private static final Random random = new Random();
    
        private static XYDataset createDataset() {
    
            final TimeSeries series = new TimeSeries("Random Data");
            Day current = new Day();
            for (int i = 0; i < N; i++) {
                series.add(current, random.nextGaussian());
                current = (Day) current.next();
            }
            return new TimeSeriesCollection(series);
        }
    
        private static JFreeChart createChart(final XYDataset dataset) {
            JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "Test", "Day", "Value", dataset, false, false, false);
            return chart;
        }
    
        public static void main(String[] args) {
    
            JFrame f = new JFrame();
            f.setLayout(new GridLayout(1, 0));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JFreeChart chart = createChart(createDataset());
            f.add(new JScrollPane(new JPanel() {
    
                {
                    setBackground(Color.red);
                }
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(SIZE, SIZE);
                }
            }));
            f.add(new ChartPanel(chart) {
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(SIZE, SIZE);
                }
            });
            f.pack();
            f.setSize(2 * (SIZE - N), SIZE - N);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have panel that is using group layout to organize some label. I want
I have a view ( Panel ) that is a card layout. I have
I have a great working 3 panel liquid layout but I just noticed that
I have css code to do layout. i have basic header panel, footer, left
I have a border layout whose center panel is defined like the below. {
I have a panel that is anchored to the top, left, right, bottom of
I have a panel. in that panel there is one text box for some
I have ExtJS View-Port panel , that contain center panel, that contain tablpanel ,
I have a Panel layout with a TreePanel in one region. A user clicks
I have a panel of labels, buttons and image that I wish to put

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.