File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Adding JScrollPane to JPanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding JScrollPane to JPanel?" Watch "Adding JScrollPane to JPanel?" New topic
Author

Adding JScrollPane to JPanel?

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
I have a problem adding a JScrollPane to JPanel. I will write some code, maybe someone could please tell me what i'm doing wrong, because i've tried a lot of things and it still does not work.



So mainly this is it, except that i have a lot of components that i add to the pane.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

You should call your constructor on the Event Dispatch Thread:
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Nope, that does not work either. Oh, i also forgot to mention, i set the dimension of the pane and of the scroll:

pane.setBounds(0, 0, 2000, 2000);
scrollp.setBounds(0, 0, 1240, 900);

right after declaring the scrollpane. Maybe this could be a problem?
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
And also, I have a panel with fixed size, the client cannot change it's dimension (i mean, he can't add or remove components or anything to or from it), so all i need is a scrollpane that appears at the beginning. I managed to add a scrollpane to many components, like textAreas or tables, but i can't manage to add it to a panel
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Why do you think that scroll-pane is not getting added? It's just that since there is nothing to scroll, the scroll-bars are not visible. The default policy for both vertical as well as horizontal scroll-bars is show-as-needed.

If you change the code a bit, then you can see the scroll-bars:

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Then I think I have a different problem than i thought. I have the window above, which contains the main panel, called "pane". To this pane, i add around 14 other panels. But when i try to add the panels that normally don't fit inside the main panel viewport, the scroll does not work. And i have no idea why
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90

this is my code. I have no idea what is wrong with it
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
I got it to work. Just had to setPreferredSize for JPanel
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Your panel's 'null' layout is the problem. Scroll-pane calculates its scroll-bars applicability using viewport's preferred size, and a panel with null layout returns (0, 0) as preferred size.

Change the code:

to:

Regards,
- Aditya
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Thanks, it works
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
So mainly this is it, except that i have a lot of components that i add to the pane.


Stop using "null layouts".

Swing was designed to be used with layout managers. One of the jobs of the the layout manager it to determine the "preferred size" of the panel once all the components are added to it.

You should NOT be setting the preferred size manually. Let the layout managers do their job.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding JScrollPane to JPanel?