192
|
Chapter 5, Windows, Dialogs, and Frames
#36 Add Status Bars to Windows
HACK
layoutCoordinateX++;
contentPanel.add(
component,
new CellConstraints(layoutCoordinateX, layoutCoordinateY)
);
}
Notice that the size is passed into this method in dialog units
rather than pixels. You could easily change this to pixels if
you want, but I find dialog units to be a much easier way to
layout this kind of component.
Running the Hack
Example 5-3 is the code for a complete simulation frame. You just need to
create the
JStatusBar and add the left, main, and right components. All of
the layout and painting logic is encapsulated completely in
JStatusBar. The
bold lines in the middle of the
StatusBarSimulator are where the status bar is
created and the three components are added. Also, notice that all of the
labels are center aligned—this mimics the standard Windows practice. You
can see the finished product in Figure 5-8.
Figure 5-8. The status bar from StatusBarSimulator
Example 5-3. Using the JStatusBar in a JFrame
public class StatusBarSimulator {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel( ));
} catch (Exception e){
}
JFrame frame = new JFrame( );
frame.setBounds(200,200, 600, 200);
frame.setTitle("Status bar simulator");
Container contentPane = frame.getContentPane( );
contentPane.setLayout(new BorderLayout( ));
JStatusBar statusBar = new JStatusBar( );
JLabel leftLabel =
new JLabel("Your application is about to self destruct.");
statusBar.setMainLeftComponent(leftLabel);

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.