Reputation: 692
So I am trying to have a JPanel inside a JScrollPane, which is located directly inside a JFrame, resize so that it is sometimes smaller than the JScrollPane. Problem is that when I try this it, the lesser size seems to be resized to the original size of the JScrollPane. It works fine when I leave it at a larger size. How do I get this to work? Code will be provided if needed.
Upvotes: 1
Views: 1453
Reputation: 324207
Wrap you panel in another panel that uses a FlowLayout. This way the outer panel will be resized to fill the viewport of the scroll pane:
JPanel red = new JPanel();
red.setBackground(Color.RED);
red.setPreferredSize( new Dimension(200, 200) );
JPanel outer = new JPanel();
outer.add( red );
add( new JScrollPane(outer) );
Upvotes: 3