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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:38:42+00:00 2026-05-30T01:38:42+00:00

I have a JFrame which holds and JScrollPane. The JScrollPane itself holds a class

  • 0

I have a JFrame which holds and JScrollPane. The JScrollPane itself holds a class derived from JPanel. I use the JPanel class to draw an Image and some other primitives on it. Unfortunately the scrollbars of the JScrollPane do not occur even though the JPanel extends over the JSrollPane size.

I created some test code which runs:

This is the main class:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

class ScrolledPane extends JFrame{

private JScrollPane scrollPane; 

public ScrolledPane()
{
    setTitle( "Scrolling Pane Application" );
    setSize( 300, 200 );
    setBackground( Color.gray );

    TestPanel testPanel = new TestPanel();
    testPanel.setLayout( new BorderLayout() );

    scrollPane = new JScrollPane(testPanel);
    this.getContentPane().add(scrollPane, BorderLayout.CENTER);
}


public static void main( String args[] )
{
    // Create an instance of the test application
    ScrolledPane mainFrame  = new ScrolledPane();
    mainFrame.setVisible( true );
}
}

And here is the code of the class derived from JPanel:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics; 
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JPanel;

public class TestPanel extends JPanel {

private Image groundPlan;

public TestPanel(){
    super();

    this.setBackground(Color.green);
    this.setLayout(new BorderLayout());
    this.groundPlan = Toolkit.getDefaultToolkit().getImage("D:\\EclipseWorkspace\\img.png");
}


public void paint(Graphics graphics) {
    super.paint(graphics);
    if(groundPlan != null) {
        graphics.drawImage(groundPlan, 0, 0, this);
    }
}
}

Interestingly the Scrollbars appear if I just insert a JLabel with and Image into the JScrollpane instead of the JPanel. Here the code for this option:

public ScrolledPane() //Frame <- Scrollpane <- label <- image 
{
    setTitle( "Scrolling Pane Application" );
    setSize( 300, 200 );
    setBackground( Color.gray );

    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );

    Icon image = new ImageIcon("D:\\EclipseWorkspace\\img.png");
    JLabel label = new JLabel( image );

    scrollPane = new JScrollPane();
    scrollPane.getViewport().add( label );
    this.getContentPane().add(scrollPane, BorderLayout.CENTER);
}

I would appriciate if you could tell how I can make the scrollbars occur if I use the JPanel in the JScrollPane.

Regards & Thanks
Marc

  • 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-30T01:38:43+00:00Added an answer on May 30, 2026 at 1:38 am

    Call setPreferredSize() for the panel in JScrollPane.

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

Sidebar

Related Questions

I have this Java JFrame class, in which I want to use a boxlayout,
I have a JFrame with an associated JPanel which fill the screen, both having
I have a class which extends JFrame and forms the GUI of my program.
I have a JFrame , which have some options. When OK button is pressed
So I have a JFrame in which inside of it I have a JScrollPane
So I have a JFrame , in which it has a bunch of JPanel
I have a window (derived from JFrame) and I want to disable the close
I have a JFrame which contains just one JPanel. I have tried setting the
I have jFrame class MainForm which contains main() , placePanel(panel) and drop down menu.
I have a JFrame inside of which is a jpanel that im using as

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.