Add Status Bars to Windows #36
Chapter 5, Windows, Dialogs, and Frames
|
189
HACK
The first section draws the gray lines at the top. Midway through, y is reset
based on the panel height, and then the bottom lines are drawn.
Add the Corner Icon
Next, you need to add the resize icon—preferably made to resemble real
Windows resize icons
[Hack #36]. Make a JLabel for the corner icon and add
the icon to it. Since this hack is mimicking the Explorer status bar, use the
TriangleSquareWindowsCornerIcon. Also, remember to set the label opacity to
false. This makes the status panel background show through the icon:
JLabel resizeIconLabel = new JLabel(new TriangleSquareWindowsCornerIcon( ));
resizeIconLabel.setOpaque(false);
Now, you need to add the label to the panel. You need the resize icon to be
at the right, on the bottom of the bar. For this to work, set the
JStatusBar
layout to a BorderLayout. Then create another panel called rightPanel and
set its
layout to BorderLayout as well. Add the rightPanel to the status bar,
to the east; add the resize icon to the
rightPanel, to the south. Don’t forget
to set the
rightPanel’s opacity to false as well. This will place the icon on
the bottom right of the status bar:
JPanel rightPanel = new JPanel(new BorderLayout( ));
rightPanel.setOpaque(false);
rightPanel.add(resizeIconLabel, BorderLayout.SOUTH);
add(rightPanel, BorderLayout.EAST);
While you’re at it, create another panel and name it contentPanel. Add this
to the center of the status bar. You’ll use this in the next few sections to add
the rest of the components:
contentPanel = new JPanel( );
contentPanel.setOpaque(false);
add(contentPanel, BorderLayout.CENTER);
Add the Left Component
Now that the contentPanel is in place, you need to configure it to hold all of
the components you’re going to add to the status bar. Remember that the
status bar has several components on the righthand side (just to the left of
the resize icon) that don’t resize.
It’s actually possible for these components to resize, but
standard Windows behavior is to have the size stay the same
and center text.
190
|
Chapter 5, Windows, Dialogs, and Frames
#36 Add Status Bars to Windows
HACK
There are also a bunch of separators between each of the righthand compo-
nents. And on the left, the status bar has a main component that takes up all
of the space left over after the righthand components (yes, it’s rather confus-
ing; read this section again and you’ll get it).
You could get most of this behavior with a bunch of nested panels and lay-
outs. But it’s easier to use a single layout manager that can handle all of
these, such as
FormLayout from JGoodies. I don’t want to get too detailed
about
FormLayout, so feel free to check out http://www.jgoodies.com for more
info. You’ll need to download JGoodies Forms from the JGoodies site and
put its JAR file (forms-1.0.5.jar as of this writing) in your classpath. Here is
the code to create a new
FormLayout and set it on the contentPanel:
layout = new com.jgoodies.forms.layout.FormLayout(
"2dlu, pref:grow",
"3dlu, fill:10dlu, 2dlu");
contentPanel.setLayout(layout);
FormLayout is essentially an advanced grid, typically configured by passing in
strings that describe the row and column contents in a heavily condensed
syntax. So, you need to configure the grid columns and rows and then add
the components to the appropriate locations. It’s a simplistic approach that
is really powerful.
The first line—
2dlu, pref:grow—creates two columns: one column is two
dialog units (
dlu in the code) wide and another column that grows. This is
for the left component (which needs to grow), and is it going to be added to
the second column (the first column is there simply to create a space). The
dialog unit system used for this space is based on the pixel size of the dialog
font, meaning it can grow and shrink as fonts and resolutions change. Over-
all, this creates better and more consistently sized components and win-
dows than using absolute pixel measurements.
The second line—
3dlu, fill:10dlu, 2dlu—creates the rows for the layout. In
this case, it adds a three dialog unit space at the top, a two dialog unit space
at the bottom, and fills the rest of the space with the component you add.
Now, add the left component. When you add components to a
FormLayout-
enabled panel, you need to use a JGoodies
CellConstraints object. You can
use the simple
CellConstraints that takes an x- and y-coordinate. In this
case, the coordinate is (2,2) because
FormLayout starts counting at 1 and
you’re skipping the first column and row to create space. Here is the code:
public void setMainLeftComponent(JComponent component){
contentPanel.add(component, new CellConstraints(2, 2));
}

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.