Add Status Bars to Windows #36
Chapter 5, Windows, Dialogs, and Frames
|
187
HACK
H A C K
#36
Add Status Bars to Windows
Hack #36
Lots of applications use a bottom-of-window panel to communicate status.
But Swing doesn’t provide a consistent way to do this. So, you need to
provide it yourself.
Many applications in Microsoft Windows use a status bar—an area at the
bottom of each window (to the left of the resize box if there is one) that can
be used to communicate summary information to the user in a compact
form. Typical contents of a status bar might include what a web browser is
doing (e.g., “Connecting to www.oreilly.com”), or, as in Figure 5-6, a sum-
mary of the contents of a folder, showing the number of contained items,
their size, etc.
Standard Status Bars
This is the standard MS Windows setup for a status bar:
An icon on the far right letting users know they can resize the application
A label on the left for free form text
Several labels on the right for details (e.g., 42.3 MB in Figure 5-6)
There is also a bit of custom painting involved to get the top and bottom
shading right. First, you’ll do the panel shading, and then loop back to the
previous list and add all of the necessary components to the status bar.
This hack copies the Windows Explorer status bar in Win-
dows XP. Different applications are slightly different. The
purpose of this hack isn’t to start a religious war about
which application to copy. You can just make minor changes
if you want to copy a different application’s status bar (such
as Word or Outlook, which are different than Windows
Explorer and also from each other).
Start by creating a class called JStatusBar extending JPanel:
public class JStatusBar extends JPanel {
//more to come
}
Then add a constructor and set the preferred height to be 23 pixels (the
height of the Windows Explorer status bar—count ’em up if you don’t trust
me). You can ignore the preferred width of the status bar since you’ll be
Figure 5-6. Windows Explorer’s status bar

Get Swing Hacks 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.