Search code examples
javaswingresizejscrollpane

JPanel in JScrollPane doesn't adjust size dynamically


I have a JScrollPane and a JPanel.

    JScrollPane scrollPane_yst = new JScrollPane();
    scrollPane_yst.setBorder(null);
    scrollPane_yst.setViewportBorder(null);
    panel.add(scrollPane_yst);

    JPanel panel_yst = new JPanel();
    panel_yst.setPreferredSize(new Dimension(150, 1000));
    panel_yst.setBorder(null);
    panel_yst.setBackground(Color.DARK_GRAY);
    scrollPane_yst.setViewportView(panel_yst);
    panel_yst.setLayout(new BoxLayout(panel_yst, BoxLayout.Y_AXIS));

During runtime, I add other components to panel_yst, but it doesn't get bigger when the components exceed its size. (Same problem with and without setting preferred size)

ANSWER:

I found the solution. The problem was, that I set the preferred size of the panels that I lateron added onto the panel inside the scrollpane.

Because of that, the size of those panels was messed up. Just dont set the PreferredSize of the components that you add onto the panel.


Solution

  • I found the solution. The problem was, that I set the preferred size of the panels that I lateron added onto the panel inside the scrollpane.

    Because of that, the size of those panels was messed up. Just dont set the PreferredSize of the components that you add onto the panel.