21

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;
    }
}

5 Answers 5

31

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);
1
  • Note that you don't have control over the scroll increment, i.e. every click on the scroll button or every mouse wheel turn results in scrolling one pixel which you need to overcome - in case you want - by implementing Scrollable. Sep 21, 2015 at 13:32
12

You have to use a JScrollPane. And then call the setViewportview(Component);

You don't have to implement scrollable, JPanel is allready scrollable

5
  • 5
    You can add any component to a JScrollPane, whether it implements Scrollable or not, but implementing Scrollable will give you more control. JPanel does not implement Scrollable, so you have to sub-class it if you want more control over how the scroll pane behaves when displaying the panel.
    – Dan Dyer
    Sep 6, 2009 at 14:25
  • (Or you can set properties on the JScrollPane/JScrollBars without implementing anything.) Sep 6, 2009 at 14:34
  • @Dan: Yes, but I never said 'implements' Sep 6, 2009 at 15:04
  • 4
    @MartijnCourteaux You never said it, but it's just too misleading. Sep 20, 2015 at 16:39
  • @MartijnCourteaux saying JPanel is allready scrollable, implicitly means that it implements the Scrollable interface, which is not true.
    – Robert
    Apr 30, 2017 at 14:28
1

As mentioned in all the other posting there is no reason to implement the Scrollable interface yourself. However, if you are just playing around, then the basic code posted looks reasonable. However you did not post your demo program showing how you use this code. In the future, post a SSCCE with your question. If you don't know what a SSCCE is then search the web.

Once possible problem is that scrollbars appear automatically when the "preferred size" of the component added to the viewport of the scrollpane is greater than the size of the scrollpane.

So if you are doing custom painting on the panel, you are responsible for setting the preferred size of the panel as it changes. If you are using a panel with components and a layout manager then you don't have to worry about this. But if you are using components with a null layout manager you will also have problems.

That is why we need a SSCCE because we don't know the context of how you are using the panel.

1

JPanel doesn't implements Scrollable. It is better to use JXPanel from SwingX, which implements Scrollable and has lot more features.

0

I have a new solution for you.

I think you have to use this code:

storyEditor = new JPanel();
storyEditor.setPreferredSize(new Dimension(..., ...)); // Insert Here your size for the editor
JScrollPane scroller = new JScrollPane(storyEditor);
tabbedPane.add("Editor", scroller));
frame.setSize(frame.getWidth()+1, frame.getHeight()); // frame is the JFrame where the tabbed pane is into
// Maybe you can replace "frame" with "this"
// You need to resize your frame. Why, I don't know...
frame.pack();
// Your original size will be restored by calling pack

This was a solution for me. I hope for you to!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.