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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:15:36+00:00 2026-05-29T05:15:36+00:00

Please see the following block of code import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import

  • 0

Please see the following block of code

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
public class test extends JFrame {
    public test(){

    this.setBounds(0,0,300,700);

    JPanel pnltemp= new JPanel();

    //pnltemp.setBounds(0,0,400,1000);
    pnltemp.setPreferredSize(new Dimension(400,1000));

    JScrollPane scrtemp= new JScrollPane();


    scrtemp.getViewport().add(pnltemp);
    this.getContentPane().add(scrtemp);

    this.getContentPane().add(scrtemp);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

}

public static void main(String args[]){
    new test();

}
}
  1. When we run the above code by commenting “pnltemp.setBounds(0,0,400,1000);” and replacing it with “pnltemp.setPreferredSize(new Dimension(400,1000));” the window shows horizontal and vertical scrollbars.
    but when we run the same programm by commenting “pnltemp.setPreferredSize(new Dimension(400,1000));” and replacing it wiht “pnltemp.setBounds(0,0,400,1000)” the window does not shows horizontal and vertical scrollbars.

why same program behaves differently by changing the setBounds and setPreferredSize method; as these two methods are looks same in behaviour.

Or is it something like this that when we are using the JScrollPane to get the Scrollbar we must use the setPreferredSize(); method for the componant that we are going to add in JScrollPane.

  1. My second question is when we add pnltemp to scrtemp i.e. the JPanel is added to JScrollPane directly then its not gives an error means when we say

scrtemp.add(pnltemp);

it does not give any error but it also does not show the pnltemp and scrollbar in scrtemp.
but when we type
scrtemp.getViewPort.add(pnltemp);

it does not give any error but it also shows the pnltemp and scrollbar in scrtemp.

I checked this by assigning the background color to JPanel and JScrollPane.

Can anybody explin this?

Thank You!

  • 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-29T05:15:38+00:00Added an answer on May 29, 2026 at 5:15 am

    From the JScrollPane JavaDocs.

    By default JScrollPane uses ScrollPaneLayout to handle the layout of its child Components. ScrollPaneLayout determines the size to make the viewport view in one of two ways:

    1. If the view implements Scrollable a combination of getPreferredScrollableViewportSize, getScrollableTracksViewportWidth and getScrollableTracksViewportHeight is used, otherwise
    2. getPreferredSize is used.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see the following code: NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@test.pdf]; NSData *myData = [NSData
Please see the following code: <body> <form id=form1 runat=server> <div> <button id=helloButton> Search</button> <div
Please see the following code and explain the output behavior. public class MyFinalTest {
folks please see following code : #include <gio/gio.h> #include <sys/socket.h> //socket(); #include <netinet/in.h> //sockaddr_in
Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {
Can someone please help me figure out a way to achieve the following (see
Please see code below. The destructors are never called. Anyone know why and how
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
Please see the following picture: Why is padding-top and margin-top fully opaque, but padding-right

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.