Search code examples
javaswingscrolljpaneljscrollpane

JPanel within JScrollPane within JPanel ignoring horizontal wrap


I have a JPanel that holds a JScrollPane that holds a JPanel as such:

masterPanel.add(buttonPanel, BorderLayout.NORTH);
inner.setLayout(new BorderLayout());
inner.add(infoPanel, BorderLayout.NORTH);
inner.add(writingPanel, BorderLayout.CENTER);
beholder = new JScrollPane(inner);

masterPanel.add(beholder, BorderLayout.CENTER);

I want beholder to be a JScrollPane so that the button panel will display at all times while scrolling the beholder (inner scroll pane). This all works fine, but the problem is the panels inside of beholder do not line wrap. So I have a long line of text in infoPanel that causes a very long, undesirable horizontal scroll.

If I just change the last line to add inner instead of beholder, it works fine (wraps correctly), but then the button panel won't stay at the top when the user scrolls down.

I'm totally stuck. Basically my question is how to get the panels inside of the JScrollPane to word wrap normally.

The problem is wrapping horizontally. Even if I set beholder.setPreferredSize() to a very low x and y dimension, it will ignore the x dimension entirely even though it seems to obey the y dimension normally.


Solution

  • For some reason, this problem is resolved by calling:

    inner.setPreferredSize(inner.getPreferredSize());
    

    This will cause the wrap to occur.