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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:33:51+00:00 2026-06-01T09:33:51+00:00

This seems like a newbie question except that I’ve been trying to wrap my

  • 0

This seems like a newbie question except that I’ve been trying to wrap my head around the Swing framework for loong time.

Provided you provide an image, dog.jpg, at least 500 px square, the following code should display the image in a scrollpane. If it displayed anything, I probably wouldn’t throw my hands up in despair. What am I missing?

import java.awt.BorderLayout;
import javax.swing.*;

public class ScrollSample {
  public static void main(String args[]) {
    String title = (args.length == 0 ? "JScrollPane Sample" : args[0]);
    new ScrollSample( title ) ;
    }

  public ScrollSample ( String title) {
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Icon icon = new ImageIcon("dog.jpg");
    JLabel dogLabel = new JLabel(icon);
    dogLabel.setSize( 500, 500 ) ;

    JLayeredPane layeredPane = new JLayeredPane() ;
    layeredPane.add( dogLabel, new Integer( 0 )) ;

    JPanel jp = new JPanel() ;
    jp.add( layeredPane ) ;
    jp.setSize( 500, 500 ) ;

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(jp);

    frame.getContentPane().add( scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Thanks!

  • 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-01T09:33:53+00:00Added an answer on June 1, 2026 at 9:33 am

    You must set the preferred size for JLayeredPane if you are drawing components of larger widths and sizes to it. Especially since you are adding it to a JPanel with default layout. JLayeredPanes don’t have layout managers by default – so either you manage the bounds or add a preferred layout manager to the layered pane. The simple way would be:

    After

     JLayeredPane layeredPane = new JLayeredPane() ;
    

    add

     layeredPane.setPreferredSize(new Dimension(500,500));
    

    And then maximize your window ( or set your JFrame‘s size to 600X600) when the app runs.

    Read up on : How to Use Layered Panes

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

Sidebar

Related Questions

This seems like a basic question, but I haven't found any clear answers. Essentially,
This seems like a basic question, but it's still bugging me. Why doesn't MyObject
This seems like a bug to me... I accept that automatic properties, defined as
This seems like it would be fairly simple, but I've been unable to find
This is a pretty newbie question- please know I'm aware of that. I have
This seems like an obvious question but there does not appear to be an
I'm sorry if this is a newbie question, but it seems I don't get
Seems like newbie's question, but I just can't find the answer. Can I somehow
This feels like a dumb newbie question. I'm attempting to follow the Palm's online
seems like a dumb question even for a VBA newbie, but I can't figure

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.