Scrollable JPanel

thoaionline picture thoaionline · Sep 6, 2009 · Viewed 52.7k times · Source

How to make a JPanel scrollable? I implemented the scrollable interface yet when adding it to the containing panel with

tabbedPane.add("Editor", new JScrollPane(storeyEditor = new MNScrollablePanel()));

nothing works

Code:

public class MNScrollablePanel extends JPanel implements Scrollable {

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }
}

Answer

Name picture Name · Jul 5, 2011

It worked with me like this....

JPanel test = new JPanel();
test.setPreferredSize(new Dimension( 2000,2000));
JScrollPane scrollFrame = new JScrollPane(test);
test.setAutoscrolls(true);
scrollFrame.setPreferredSize(new Dimension( 800,300));
this.add(scrollFrame);