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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:45:55+00:00 2026-05-30T04:45:55+00:00

I have a JFrame, in this JFrame I have a JPanel that I draw

  • 0

I have a JFrame, in this JFrame I have a JPanel that I draw on, this Panel can be any size and so I placed it into a JScrollpane to let me scroll when the panel is larger than the window screen size.

Unfortunately does not work as I expected:

  1. Making the JFrame window smaller than the JPanel size does not show scroll bars

  2. The JScrollPane size now seems locked to the size of the JPanel I have added to it, where as before it resized to the bounds of it’s JFrame window (it still kinda does this but only vertically now?!)

  3. The JPanel seems to assume the size of the JScrollpane regardless of what I set for preferred size

I am sure I’m doing something stupid, if someone could point out what I would be most grateful!

JPanel imageCanvas = new JPanel(); // 'Canvas' to draw on
JScrollPane scrollPane = new JScrollPane();
    
// set size of 'canvas'
imageCanvas.setMinimumSize(new Dimension(100,100));

// Scroll pane smaller then the size of the canvas so we should get scroll bars right?
scrollPane.setMinimumSize(new Dimension(50,50)); 
    
// Add a border to 'canvas'
imageCanvas.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    
scrollPane.setViewportView(imageCanvas);
  • 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-30T04:45:56+00:00Added an answer on May 30, 2026 at 4:45 am

    setPreferredSize() is the trick, setMinimumSize() and even setSize() on the component will be ignored by JScrollPane. Here’s a working example using a red border.

    import java.awt.*;
    
    import javax.swing.*;
    
    public class Scroller extends JFrame {
    
        public Scroller() throws HeadlessException {
            final JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.red));
            panel.setPreferredSize(new Dimension(800, 600));
    
            final JScrollPane scroll = new JScrollPane(panel);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
            add(scroll, BorderLayout.CENTER);
            setSize(300, 300);
            setVisible(true);
        }
    
        public static void main(final String[] args) throws Exception {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Scroller().setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one's a tough one - I have a JFrame that generates JTextFields. When
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a JFrame that contains a display JPanel with JTextField and a control
I have a problem making an inner class that extends from JPanel to draw
I currently have a JScrollPane in a JPanel. I have Images that are loaded
I have a component that inherits from JPanel, I draw a grid on it.
I have a JPanel that is not part of a JFrame. For various reasons
I have a JFrame inside of which is a jpanel that im using as
I'm working in Java, and I have a JPanel in a JFrame. In that
I have a JFrame and a Jpanel over that in which various buttons are

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.