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

The Archive Base Latest Questions

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

I have a JPanel with a custom LayoutManager and trying to get it inside

  • 0

I have a JPanel with a custom LayoutManager and trying to get it inside a JScrollPane, however, the scroll pane doesn’t seem to know it’s supposed to enable scrolling. No matter how I set preferred size of the components (that is what the javadoc for JScrollPane says it needs), it makes absolutely no difference. Here is the code:

    canvas = new JPanel();
    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(canvas);
    scroll.setPreferredSize(new Dimension(924,700));
    canvas.setPreferredSize(new Dimension(2000,2000));

The scrollbars don’t appear. The container of the JScrollPane is set to respect its preferred size. I have checked through the debugger that the preferred size of the canvas is indeed 2000 x 2000 at all times (the LayoutManager ensures this), so what might be going on here?

  • 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-30T06:20:24+00:00Added an answer on May 30, 2026 at 6:20 am

    This is not really an answer, but is a demo sscce that shows that again, your code works fine for me:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Paint;
    import java.awt.RenderingHints;
    
    import javax.swing.*;
    
    public class ScrollBarEg {
       protected static final Paint GRADIENT_PAINT = new GradientPaint(0, 0,
             Color.blue, 50, 50, Color.red, true);
    
       private static void createAndShowGui() {
          JPanel mainPanel = new JPanel();
    
          JPanel canvas = new JPanel() {
             @Override
             protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setPaint(GRADIENT_PAINT);
                g2.fillOval(0, 0, getWidth(), getHeight());
                g2.dispose();
             }
          };
          JScrollPane scroll = new JScrollPane();
          scroll.setViewportView(canvas);
          scroll.setPreferredSize(new Dimension(924, 700));
          canvas.setPreferredSize(new Dimension(2000, 2000));
    
          mainPanel.add(scroll);
    
          JFrame frame = new JFrame("ScrollBar Eg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    This suggests to me that your error lies elsewhere in code that you’ve not posted.

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

Sidebar

Related Questions

Let's say you have a JPanel size 200 x 200, with a custom combo-box-type
I have a custom component subclassed from JPanel, with a keyboard handler. My main
I have A Jpanel with A GridLayout inside. Now I added another Jpanel inside
I'm making a custom ListCellRenderer. I know that you can have different dimensions for
I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel
I have a custom JPanel and sometimes throughout my program, I need to call
I'm trying to write custom JFrame and JPanel for my Java application. Currently, I
I have a JTable with a custom cell renderer. The cell is a JPanel
I have a popup menu that displays dynamically created custom JPanel objects in a
So I have a JFrame which I've added a custom JPanel component. The JPanel

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.