This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Manually sizing JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Manually sizing JScrollPane" Watch "Manually sizing JScrollPane" New topic

Manually sizing JScrollPane

Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Hey all,

I am working on a GUI, and in it is a JScrollPane. In the ScrollPane is a JPanel that holds multiple JTextPanes. That way there will be multiple viewable TextPanes in one scrollpane. However I want the scrollpane to not show all of the Panel that it contains; I want it to be a smaller size. I have tried putting the scrollpane in another panel, which goes in my applet, but the panel resizes because it is in a layout manager. How would I be able to make the size of the viewport set to what I want it to be?

Thanks for any help,

Darryl Burke

Joined: May 03, 2008
Posts: 4523

You're not going to like my answer. By using an appropriate layout manager, or a combination of layout managers, possibly in conjunction with EmptyBorders.

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

luck, db
There are no new questions, but there may be new answers.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Ok thanks Darryl,
Heres an example of what I'm talking about

Thanks for your help,
Rob Spoor

Joined: Oct 27, 2005
Posts: 19557

An applet is never an SSCCE, as I need an HTML page to be able to display it.

How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
An applet is never an SSCCE, as I need an HTML page to be able to display it.

I also agree with this.

I'm not sure how IDE's work, but when using the command line there is a simple solution provided the poster adds the following as the first line to the posted source code:

Now from the command line you can do the following:


However, I agree the safest way to post code is to post an application as many people will not go to the trouble of testing applets.

Anyway, back to the question. As mentioned by Darryl its a matter of understanding how layout managers work. You set the preferred size of the wrong component:

Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Yeah sorry about posting an applet instead of an application. While i was making the application however, I put in the code from Rob, and I got it to work.

Thanks guys,
I agree. Here's the link:
subject: Manually sizing JScrollPane
Similar Threads
Tree and drawing within the same panel
Need Help resizing my JScrollPane
Increasing the size of the Panel
JTable in JScrollPane sizeing
Why did the scrollbar is not available?