Scrollable JDesktopPane?

Question!

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?



Answers

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()
  {
    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.

By : Atta


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 !

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