18

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?

Oli
  • 919
  • 8
  • 16
phunehehe
  • 8,618
  • 7
  • 49
  • 79

8 Answers8

26

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).

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
  • 1
    The contents are `JPanel`s of fixed size, and they are not auto rearranged. The scrollbar simply disappear :( – phunehehe Nov 13 '09 at 16:16
  • `JScrollPane.HOTRIZONTAL_SCROLLBAR_NEVER` is the variable you have to pass to it if you want to skip that import. Matthew's solution works for me, thanks. – Pangolin May 26 '11 at 15:21
  • 1
    Other components should Implement Scrollable and let getScrollableTracksViewportWidth() return false as mentioned below. – Erik Martino Nov 27 '15 at 14:04
10

Finally I found out how, please see this very short example, no advanced tricks needed:

public class MyTest extends JFrame {

    public static void main(String[] args) {
        MyTest test = new MyTest();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(300, 200);
        test.setVisible(true);
    }

    public MyTest() {
        String[] data = {
            "Arlo", "Cosmo", "Elmo", "Hugo",
            "Jethro", "Laszlo", "Milo", "Nemo",
            "Otto", "Ringo", "Rocco", "Rollo"
        };
        JList list = new JList(data);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        list.setVisibleRowCount(0);
        JScrollPane scroll = new JScrollPane(list);
        this.add(scroll);
    }
}

Adapted from Sun's tutorial

phunehehe
  • 8,618
  • 7
  • 49
  • 79
7

This is no custom solution out of the box in the JDK.

You can use the WrapLayout.

Or you can create a custom panel and implement the Scrollable interface. The key in this case is overriding getScrollableTracksViewportWidth() to return true, so the viewports width and not the width of the panel is used for layout purposes. An example of this approach can be found in the ScrollableFlowPanel

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    I don't like the idea of using a third party library. Java should have that by default I think. But thanks for sharing, it does what I need already :) – phunehehe Nov 25 '09 at 11:45
1

You can use org.jdesktop.swingx.JXPanel instead of javax.swing.JPanel as the ViewportView of your JScrollPane:

1. download "SwingX 1.6.4 All - Binary" , and add the jar to your project's Libraries here.

2. set JXPanel as the ViewportView of your JScrollPane instead of a JPanel:

JScrollPane scrollPane = new JScrollPane();  
JXPanel panel = new JXPanel();  
scrollPane.setViewportView(panel);

3. add following two statements:

panel.setScrollableTracksViewportHeight(false);  
panel.setScrollableTracksViewportWidth(true);

4. then you can add elements to JXPanel as you did to a JPanel

Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
cmicat
  • 347
  • 3
  • 5
0

try this one, this works fine on me jScrollPane1.setHorizontalScrollBar(null);

-2

Just delete isScrollableH = percentInViewH > 1; in jquery.jscrollpane.js

whitesiroi
  • 2,725
  • 4
  • 30
  • 64
-2

I came across this issue as well... my solution was to modify the JScrollPane source code and pass an additional parameter to disable horizontal scrolling. The edits are really simple:

On line 53:

$.fn.jScrollPane = function (settings, horizontalScrollingEnabled) {

Then, on line 177:

if (horizontalScrollingEnabled) {
isScrollableH = percentInViewH > 1;
} else {
isScrollableH = false;
}

That's all you need to change. Minify the modified source code and include it on your page. When initializing the jScrollPane, you can now set whether you want it to scroll horizontally like so:

$('.scroll-pane').jScrollPane("", false);

Hope that helps.

-2

Just set the display to "none" using the CSS class for the horizontal bar. At the bottom of your jScrollPlane (after you've initialized it), add in:

$('.jspHorizontalBar').css({ 'display': 'none' });

If using multiples on a page, and some that NEED the horizontal bar, you can always edit your targeting to include the parent element.

aBenChase
  • 29
  • 1