Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollable JPanel

Tags:

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;
    }
}
like image 596
thoaionline Avatar asked Sep 06 '09 13:09

thoaionline


People also ask

How do I make my JPanel scrollable?

The important methods of a JPanel class are getAccessibleContext(), getUI(), updateUI() and paramString(). We can also implement a JPanel with vertical and horizontal scrolls by adding the panel object to JScrollPane.

Is JPanel scrollable?

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

What is the difference between JScrollPane and JScrollBar?

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.


2 Answers

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);
like image 50
Name Avatar answered Sep 19 '22 17:09

Name


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

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

like image 43
Martijn Courteaux Avatar answered Sep 21 '22 17:09

Martijn Courteaux