Greg Mattes
Greg Mattes

Reputation: 33939

Scrollable JDesktopPane?

I'd like to add scrolling capability to a javax.swing.JDesktopPane. But wrapping in a javax.swing.JScrollPane does not produce the desired behavior.

Searching the web shows that this has been an issue for quite some time. There are some solutions out there, but they seem to be pretty old, and I'm not not completely satisfied with them.

What actively maintained solutions do you know?

Upvotes: 13

Views: 4822

Answers (3)

Atta
Atta

Reputation: 1

Javaworld's JScrollableDesktopPane is no longer available on their website. I managed to scrounge up some copies of it but none of them work.

A simple solution I've derived can be achieved doing something like the following. It's not the prettiest but it certainly works better than the default behavior.

public class Window extends Frame {
    JScrollPane scrollContainer = new JScrollPane();
    JDesktopPane mainWorkingPane = new JDesktopPane();

    public Window() {
        scrollContainer.setViewportView(mainWorkingPane);

        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent evt) {
                revalidateDesktopPane();
            }
        });
    }

    private void revalidateDesktopPane() {
        Dimension dim = new Dimension(0,0);
        Component[] com = mainWorkingPane.getComponents();
        for (int i=0 ; i<com.length ; i++) {
            int w = (int) dim.getWidth()+com[i].getWidth();
            int h = (int) dim.getHeight()+com[i].getHeight();
            dim.setSize(new Dimension(w,h));
        }
        mainWorkingPane.setPreferredSize(dim);
        mainWorkingPane.revalidate();
        revalidate();
        repaint();  
    }
}

The idea being to wrap JDesktopPane in a JScrollPane, add a resize listener on the main Frame and then evaluate the contents of the JDesktopPane on resize (or adding new elements).

Hope this helps someone out there.

Upvotes: 4

Benj
Benj

Reputation: 1204

I've found this : http://www.javaworld.com/javaworld/jw-11-2001/jw-1130-jscroll.html?page=1

It's a nice tutorial with lots of explanations and infos on Swing & so, which permits to create a JscrollableDesktopPane with lots of stuff.

You will need to modify a bit some parts of code to fulfill your requirements.

Enjoy !

Upvotes: 1

Swati
Swati

Reputation: 52707

I've used JavaWorld's solution by creating my own JScrollableDesktopPane.

Upvotes: 8

Related Questions