Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes How to make my JPanel scrollable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make my JPanel scrollable" Watch "How to make my JPanel scrollable" New topic
Author

How to make my JPanel scrollable

Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37
Hi guys!

I have created a JFrame in a new application.

The JFrame content pane is a simple JPanel with no layout.

The code looks like that:



I have added a lot of components to contentPane, so the height of all these components is bigger than the Frame height.

Ho can I add a scroll bar to my Jpanel, in the way that all the components will be displayed by a user?

I have read something abount JScrollPane, but I am not able to obtain the result...

TY and BR,

Adriano.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have read something abount JScrollPane, but I am not able to obtain the result...

why not?/what happened?/post the code you've tried
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Start in the Java Tutorials.
Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37
Hi have try to put the Jpanel, that contains all my components, into a JScrollablePane, in this way:



Nothing is showed...
Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37
I have read the tutorial: that's what I've learn:

"Here's the code that creates the text area, makes it the scroll pane's client, and adds the scroll pane to a container:"

So, in my case, a Jpanel is the ScrollPane's client.

Than, I have added the scollPane to my container (JFrame)...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> JScrollPane scrollPane = new JScrollPane(contentPane);
this is correct

> scrollPane.add(contentPane);
this will never work (it's scrollPane.setViewportView(contentPane) - excuse typos)

this is likely to be the problem:
> The JFrame content pane is a simple JPanel with no layout.

the basics of a scrollpane is that the preferredSize of the component in the viewport
must be bigger than the preferredeSize of the scrollpane - i.e. if the panel has no layout,
then it has no preferredSize = you must set it.
Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37
It works!!!

TY!!!
 
GeeCON Prague 2014
 
subject: How to make my JPanel scrollable