
210
|
Chapter 5, Windows, Dialogs, and Frames
#40 Minimize to a Mini-Frame
HACK
The code declares a MiniMizeHack class, which creates the UI and adds itself
as an
ActionListener to the Minimize and Restore menu items. The mouse
listener implementation is there to control the pop-up menu, but because
the
MiniMizeHack
class hasn’t been added as a listener to any components,
the pop up won’t do anything yet.
Minimize the Frame
The actionPerformed( ) method does the actual switching. This is the meat
of the hack. It tests if the bottom component is visible. If the component is
visible, then this method calls
switchToMini( )
and reshapes the frame to be
smaller. If the bottom component is not visible, then
actionPerformed( )
calls
switchToNormal( )
to reverse the changes:
public void actionPerformed(ActionEvent evt) {
if(bottom.isVisible( )) {
switchToMini( );
} else {
switchToNormal( );
}
}
The magic happens in the switchToMini( ) method. A big part of a mini win-
dow is that it doesn’t have any borders, or at the least it uses custom ones.
Swing does not let you turn off a frame’s borders and window decorations
after the frame has been shown on screen because it might have already allo-
cated immutable system resources. The only way around this limitation is to
seamlessly replace the old frame with a new one:
private Dimension normal_size;
public void switchToMini( ) {
// nuke the old frame and build a new one ...