phunehehe
phunehehe

Reputation: 8778

disable horizontal scroll in jscrollpane

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?

Upvotes: 18

Views: 52288

Answers (8)

erica may
erica may

Reputation: 1

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

Upvotes: 0

Matthew Murdoch
Matthew Murdoch

Reputation: 31493

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

Upvotes: 26

cmicat
cmicat

Reputation: 357

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

Upvotes: 1

whitesiroi
whitesiroi

Reputation: 2853

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

Upvotes: -3

aBenChase
aBenChase

Reputation: 27

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.

Upvotes: -3

Kevin Parkerson
Kevin Parkerson

Reputation: 11

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.

Upvotes: -3

phunehehe
phunehehe

Reputation: 8778

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

Upvotes: 10

camickr
camickr

Reputation: 324207

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

Upvotes: 7

Related Questions