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.
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
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:
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
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.
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: