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;
    }
}
thoaionline
  • 518
  • 2
  • 5
  • 14

5 Answers5

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);
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Name
  • 311
  • 1
  • 3
  • 2
  • 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`. – Kalle Richter Sep 21 '15 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

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 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 06 '09 at 14:25
  • (Or you can set properties on the `JScrollPane`/`JScrollBar`s without implementing anything.) – Tom Hawtin - tackline Sep 06 '09 at 14:34
  • @Dan: Yes, but I never said 'implements' – Martijn Courteaux Sep 06 '09 at 15:04
  • 4
    @MartijnCourteaux You never said it, but it's just too misleading. – Kalle Richter Sep 20 '15 at 16:39
  • @MartijnCourteaux saying `JPanel is allready scrollable`, implicitly means that it implements the Scrollable interface, which is not true. – Robert Apr 30 '17 at 14:28
1

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

Tom Wright
  • 11,278
  • 15
  • 74
  • 148
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.

camickr
  • 321,443
  • 19
  • 166
  • 288
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!

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287