Add Status Bars to Windows #36
Chapter 5, Windows, Dialogs, and Frames
|
191
HACK
Add a Separator Panel
Before you can add the components to the right side, you need to make a
separator panel. This is actually a really simple component that draws a gray
line and a white line in the middle of the panel. Look on the right and left of
the 42.3 MB text in Figure 5-7 to see what the separators should look like.
Example 5-2 shows the
SeparatorPanel
code.
And the Rest...
Now that the right panel has been added and you’ve got the code for the
SeparatorPanel, you can add the rest of the components to the status bar.
You’ll be adding two columns to the layout—one for the
SeparatorPanel
and one for the actual component. In order to add the components at the
correct location, you’ll need to cache the
x- and y-coordinates and incre-
ment the coordinates as you add components. So, create two fields—
layoutCoordinateX and layoutCoordinateY—to keep track of the current
coordinates:
public void addRightComponent(JComponent component, int dialogUnits){
layout.appendColumn(new ColumnSpec("2dlu"));
layout.appendColumn(new ColumnSpec(dialogUnits + "dlu"));
layoutCoordinateX++;
contentPanel.add(
new SeparatorPanel(Color.GRAY, Color.WHITE),
new CellConstraints(layoutCoordinateX, layoutCoordinateY)
);
Example 5-2. A panel to separate sections of the status bar
public class SeparatorPanel extends JPanel {
private Color leftColor;
private Color rightColor;
public SeparatorPanel(Color left, Color right) {
this.leftColor = left;
this.rightColor = right;
setOpaque(false);
}
protected void paintComponent(Graphics g) {
g.setColor(leftColor);
g.drawLine(0,0, 0,getHeight( ));
g.setColor(rightColor);
g.drawLine(1,0, 1,getHeight( ));
}
}

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.