Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Create a scrollable virtual desktop in Swing

Enhance your Java GUIs with the JScrollableDesktopPane class

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
The JDesktopPane class, first introduced in JDK 1.2 as a subsidiary to Swing's GUI component series, lets you create a virtual desktop or Multiple Document Interface (MDI) in Java applications. JInternalFrame's various child windows or internal frames populate this desktop, and because those frames are internal, they are clipped at the boundary of the JDesktopPane container class (as opposed to JFrame's external frames, which are painted without regard to container boundaries).

This clipping, demonstrated in Figure 1, exemplifies one of JDesktopPane's inherent limitations: a user cannot view an internal frame's hidden portion without dragging the frame back within the virtual desktop boundary, or resizing the JDesktopPane container itself. Needless to say, such actions are not conducive to navigability and usability.

Figure 1. Internal frame clipped by the virtual desktop. Click on thumbnail to view full-size image.

JDesktopPane's second limitation is that it doesn't provide a simple method to switch between internal frames; instead, you must click upon the frame title bar. Should internal frames obscure one another, the user must drag each frame aside before the next one becomes accessible. This work becomes tedious if several internal frames overlap, as is possible in any MDI environment.

Introducing JScrollableDesktopPane

The JScrollableDesktopPane class presented in this article offers a solution to the aforementioned clipping and overlap problems, and mimics the interface of the original JDesktopPane class in order to easily upgrade your application. Figure 2 depicts the scrollable desktop pane in action. As Figure 2 shows, JScrollableDesktopPane involves three main subcomponents: a virtual desktop, a toolbar, and a menu.

Figure 2. The JScrollableDesktopPane class in action. Click on thumbnail to view full-size image.

The virtual desktop comprises the main display area. When internal frames are positioned outside the virtual desktop's boundary, scroll bars update to provide access to the cropped internal frames, solving the clipping problem.

A toolbar provides a lengthwise set of toggle buttons above the virtual desktop, with each button mapped to a corresponding internal frame. The toolbar contents automatically size to fit as you add or remove buttons. When you click a button, the associated frame centers upon the virtual desktop and positions atop all other internal frames, solving the accessibility problem.

You may register a menu bar with JScrollableDesktopPane so that your application can offer an alternative solution to the accessibility problem. Upon registration, a Window menu is added to the main application's menu bar. This menu contains Tile, Cascade, Auto, and Close options along with a set of radio buttons that serve as dynamic shortcuts to the internal frames. Tile saturates the desktop with a tiled version of all internal frames, as shown in Figure 3.

Figure 3. Internal frames displayed in Tile mode. Click on thumbnail to view full-size image.

Cascade positions each internal frame in diagonal succession, as shown in Figure 4.

Figure 4. Internal frames displayed in Cascade mode. Click on thumbnail to view full-size image.

Auto allows the user to automatically tile or cascade new internal frames (default is auto-cascade), and Close disposes of the active internal frame.

Implement JScrollableDesktopPane

As you read the implementation details that follow, keep the UML class diagram in Figure 5 handy. You can click on the image for the full implementation details.

Figure 5. UML class diagram of JScrollableDesktopPane at a conceptual level. Click on thumbnail to view the implementation-level UML class diagram.

The JScrollableDesktopPane class is a JPanel subclass built upon five major GUI components: the BaseDesktopPane, DesktopScrollPane, BaseInternalFrame, DesktopResizableToolbar, and DesktopMenu component classes. These GUI components are labeled in Figure 6.

Figure 6. JScrollableDesktopPane with major GUI components labeled. Click on thumbnail to view full-size image.

The BaseDesktopPane and DesktopScrollPane classes comprise the virtual desktop. BaseDesktopPane, a JDesktopPane subclass, is located within a container of DesktopScrollPane, a JScrollPane subclass. The JScrollPane Swing component provides a scrollable view replete with horizontal and vertical scroll bars. When you move or resize an internal frame within the BaseDesktopPane class, a ComponentListener event fires. This event updates the DesktopScrollPane class's scroll bars via manipulation of the BaseDesktopPane preferred size (set via the setPreferredSize() method). The dimensions of this preferred size are determined from the minimum and maximum extents of all internal frames upon the desktop:

   Rectangle viewP = getViewport().getViewRect();
   int maxX=viewP.width+viewP.x, maxY=viewP.height+viewP.y;
   int minX=viewP.x, minY=viewP.y;
   JInternalFrame f = null;
   JInternalFrame[] frames = getAllFrames();
   for (int i=0; i < frames.length; i++) {
      f = frames[i];
      if (f.getX() < minX) { // get min X
         minX = f.getX();
      }
      if ((f.getX() + f.getWidth()) > maxX) { // get max X
            maxX = f.getX() + f.getWidth();
      }
      if (f.getY() < minY) { // get min Y
         minY = f.getY();
      }
      if ((f.getY() + f.getHeight()) > maxY) { // get max Y
            maxY = f.getY() + f.getHeight();
      }
   }


This technique is similar to that employed by the ScrollDemo2 example found in The Java Tutorial.

Internal frames added to the virtual desktop are of class BaseInternalFrame, a JInternalFrame subclass. The BaseInternalFrame class provides the getter and setter methods necessary to fetch any associated toggle and menu buttons (i.e., get/setAssociatedMenuButton() and get/setAssociatedButton() methods). When you minimize an internal frame, a blank image replaces the icon, as you access minimized frames via the toolbar's toggle buttons and any icon images only clutter the desktop.

The DesktopResizableToolbar class comprises the toolbar in Figure 6. A ResizableToolBar (not depicted in Figures 5 and 6) subclass, DesktopResizableToolbar renders a generic toolbar whose contents automatically size to fit as you add or remove buttons. ResizeableToolBar itself subclasses JToolBar, a Swing component class that provides a container for toolbar buttons. Upon a button's addition or removal (or a toolbar resizing), the ResizeableToolBar class dynamically determines the width of the remaining buttons by dividing the total container width by the button number:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources