disable horizontal scroll in jscrollpane

phunehehe picture phunehehe · Nov 13, 2009 · Viewed 46.8k times · Source

I have a JScrollPane with FlowLayout that I want to have a fixed width. It should be scrolling vertically only, and the contents should be rearranged automatically when I resize the window. I think there should be a method like setEnableHorizontalScroll(false) but can't find it.

Is there any easy way to do this?

Answer

Matthew Murdoch picture Matthew Murdoch · Nov 13, 2009

You can use:

import static javax.swing.ScrollPaneConstants.*;
// ...
JScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);

...but this just prevents the horizontal scrollbar from ever being shown.

To get the contents to rearrange automatically will depend on what the content is. A JTextPane or JEditorPane will do this for you automatically (without the need for the above code).