Java Reference
In-Depth Information
Directory "Sketcher 1 with a status bar"
The StatusPane constructors call different JLabel constructors, one with and one without an Icon ar-
gument. The last argument to the JLabel constructors is a value of type int that determines the alignment
of what is displayed in the label. You can set the contents to be aligned left, centered, or aligned right by
specifying the LEFT , CENTER , or RIGHT constants from the SwingConstants interface that the JLabel class
implements. I'm choosing left-alignment for the status bar pane content in both cases.
The StatusPane constructors call a helper method, setupPane() , that contains code that is common to
both. This sets up the physical appearance of a pane. It first sets the background and foreground colors, the
latter being the color of any text that is displayed. It then sets the font for displayed text to be the class mem-
ber, font . This is a standard 10-point serif font, but you could choose a different font if you like.
The panes have a compound border. The outside border is a lowered bevel border that distinguishes each
pane clearly. The inside border is an empty border that serves to set the distance of the contents that you
display in a pane from the outside border. The four arguments in sequence specify the distances from the
top, left, bottom, and right edges respectively.
If you can maintain a fixed width for each pane, it prevents the size of the pane from jumping around
when you change the text. To do this you call the inherited setPreferredSize() method. The argument is a
java.awt.Dimension object, and the first argument to the constructor is the width and the second argument
is the height. The values are such to accommodate the maximum length of contents you want to display with
the specified font.
Now you know the detail of the StatusPane class, you can update the definitions of the colorPane and
typePane members of the StatusBar class:
private StatusPane colorPane = new StatusPane("BLUE", BLUE16);
private StatusPane typePane = new StatusPane("LINE");
The colorPane member has text and an icon as the contents. The icon is the one you used on the corres-
ponding menu item. These are 16 × 16 so they fit within the height of a pane. The typePane member just
displays the current element type as text. The longest text to be displayed is “Rectangle,” and this fits within
the width of a pane as a 10-point font. They are created to correspond with the initial element type and color.
Updating the Panes
You can code the setColorPane() method as follows:
// Set color pane contents
public void setColorPane(Color color) {
String text = null;
// Text for the color pane
Icon icon = null;
// Icon to be displayed
if(color.equals(Color.RED)) {
text = "RED";
icon = RED16;
} else if(color.equals(Color.YELLOW)) {
text = "YELLOW";
icon = YELLOW16;
Search WWH ::




Custom Search