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?


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 behaviour.

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

  public Window()

    addComponentListener(new ComponentAdapter()
      public void componentResized(ComponentEvent evt)

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

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.

By : Atta

I've found this :

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 !

By : Benj

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

By : Swati

This video can help you solving your question :)
By: admin