Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding JScrollPane to JPanel?

 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6322
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should call your constructor on the Event Dispatch Thread:
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is my code. I have no idea what is wrong with it
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it to work. Just had to setPreferredSize for JPanel
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it works
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic