• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Adding JScrollPane to JPanel?

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it to work. Just had to setPreferredSize for JPanel
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it works
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic