I am creating a JPanel form which will contain several other JPanels. I want to place this inside a JScrollPane. Then I want to place the JScrollPane into a JTabbedPane as one of the tabs. I'm having a problem though -- my JPanel form winds up expanding when placed in the scrollpane even though I have set size, preferredsize, maximumsize, etc.
Asked
Active
Viewed 1,576 times
0
-
2It could be me, but I'm finding it very hard to discern exactly what you're trying to achieve vs. what you're getting as the descriptions just don't paint an adequate visual picture for me. Perhaps you can upload images of what you want, what you're getting, and a small compilable program to demonstrate what you're trying to do. Perhaps you want to nest your "JPanel form" into another JPanel using an appropriate layout manager, and then adding this other JPanel as the JScrollpane's viewport view. – Hovercraft Full Of Eels Aug 21 '11 at 17:59
-
Here's an [example](http://stackoverflow.com/questions/3174765/variable-layout-in-swing/3175280#3175280) of multiple panels in a scroll pane that might get you started. – trashgod Aug 21 '11 at 20:09
1 Answers
1
public class test
{
private static JFrame frame = new JFrame();
private static JTabbedPane pane0 = new JTabbedPane();
private static JScrollPane pane1 = new JScrollPane();
private static JPanel pane2 = new JPanel();
//add the rest of your JPanels here
public static void main(String[] args)
{
frame.setSize(400,400);
//add all the other attributes here
frame.add(pane0);
pane0.add(pane1);
pane1.add(pane2);
//go ahead and add the rest of your panels here
frame.pack();//resizes the frame so that its subcomponents fit well inside.
}
}//this last bracket is for the class itself. Sorry i couldn't tab everything the right //way.
Is this what you're trying to do? That's what i understood from your question. By the way, if your JPanel is expanding, change the size of your frame as well.

corecase
- 1,278
- 5
- 18
- 29
-
2I would expect to [`pack()`](http://download.oracle.com/javase/6/docs/api/java/awt/Window.html#pack%28%29) the frame's `Window` "to fit the preferred size and layouts of its subcomponents." – trashgod Aug 22 '11 at 00:21
-
You're right, that would work out perfectly i think. I'll go ahead and edit my answer. Thanks for the input. – corecase Aug 22 '11 at 03:30
-
Yes, pretty much. I've traced my issue to the GridLayout manager. It expands to fill ALL available space, a fact of which I'd been unaware. So here is my question: I want to create a JPanel that contains three other JPanel forms created with the GridLayout manager. But the scrollbar never appears because they are always automatically sized to fit. @Corecase – Marc H Aug 23 '11 at 13:59
-
1@Marc Here's what you would do: private static JPanel mainPane = new JPanel(); private static JPanel pane1 = new JPanel(); private static JPanel pane2 = new JPanel(); private static pane3 = new JPanel(); Now, in your main() method you would do the following: frame.add(mainPane); mainPane.setLayout(new GridLayout(3,1)); mainPane.add(pane1); mainPane.add(pane2); mainPane.add(pane3); The above would give you a panel with three other panels stacked on top of each other on the left-most column of your main panel(mainPane). – corecase Aug 23 '11 at 17:20
-
Ignore the stacked part; they're not "stacked", just one is above the other and they take up the entire left column. – corecase Aug 23 '11 at 17:27