Scrollable JPanel

2019-02-08 19:51发布

问题:

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

回答1:

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

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



回答2:

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


回答3:

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.



回答4:

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



回答5:

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!