Building a Desktop

In this section, we’ll pull together some of the things we’ve discussed in the previous pages to create an application using JDesktopPane, JInternalFrame, and a custom DesktopManager. The example shows:

  • The effect of adding frames to different layers of the desktop.

  • How to display a background image (“wallpaper”) on the desktop.

  • How to keep frames from being moved outside of the desktop.

  • How to deiconify, move, and resize internal frames by frame “tiling.”

  • How to take advantage of JInternalFrame’s constrained properties by requiring that there be at least one non-iconified frame on the desktop.

Figure 9.4 shows what the application looks like when it’s running.

SampleDesktop layered frames and background image

Figure 9-4. SampleDesktop layered frames and background image

Here we see the desktop with three frames, plus a fourth that has been iconified. The frames titled “Lo” are in a lower layer than the “Up” frame. No matter which frame is active or how the frames are arranged, the “Up” frame will always appear on top of the others. Frames in the same layer can be brought to the front of that layer by clicking on the frame. This display also shows the use of a background image (what good is a desktop if you can’t put your favorite image on the background, right?). This image is added to a very low layer (the lowest possible integer, actually) to ensure that it is always painted behind anything else in the desktop. ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.