O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

208
|
Chapter 5, Windows, Dialogs, and Frames
#40 Minimize to a Mini-Frame
HACK
I Shall Call Him...Mini-Me
Switching a frame between two sizes is quite easy: just call setSize( ) and
you’re done. Doing it well is a bit more difficult, however. When you mini-
mize the window, you also need to remove the window decorations, hide
the menu bar, and remove the components that shouldn’t be visible in the
mini-view. This is a bit trickier, not the least of which because you can’t turn
off the window decorations of a frame once it has been created. But I’m get-
ting ahead of myself. First, you need a sample application.
Let’s take a simple clock program. The normal window looks like
Figure 5-11. The goal is to provide a mini version that looks like Figure 5-12.
This program has a clock, a panel with more configuration options (repre-
sented here with just the label More configuration), a menu bar, and a pop-
up menu for later use. Example 5-9 creates the interface and puts the com-
ponents in the right places, but it doesn’t do anything with them yet.
Figure 5-11. A normal application window
Figure 5-12. A mini application window
Example 5-9. The beginning of a clock with a mini version
public class MiniMizeHack implements MouseListener, ActionListener {
public JFrame frame;
public JPanel panel;
public JPopupMenu popup;
Minimize to a Mini-Frame #40
Chapter 5, Windows, Dialogs, and Frames
|
209
HACK
public JMenuBar menubar;
public JLabel top;
public JLabel bottom;
public MiniMizeHack( ) {
top = new JLabel(new ImageIcon("image.png"));
bottom = new JLabel("More configuration here");
frame = new JFrame("Mini Mize Hack");
panel = new JPanel( );
panel.setLayout(new BorderLayout( ));
panel.add("Center",bottom);
panel.add("North",top);
frame.getContentPane( ).add(panel);
menubar = new JMenuBar( );
JMenu menu = new JMenu("File");
menu.add(new JMenuItem("Open"));
menu.add(new JMenuItem("Quit"));
menubar.add(menu);
JMenu window = new JMenu("Window");
JMenuItem mini = new JMenuItem("Minimize");
mini.addActionListener(this);
window.add(mini);
menubar.add(window);
frame.setJMenuBar(menubar);
popup = new JPopupMenu( );
JMenuItem restore = new JMenuItem("Restore");
restore.addActionListener(this);
popup.add(restore);
}
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger( )) {
popup.show(e.getComponent( ),
e.getX(), e.getY( ));
}
}
Example 5-9. The beginning of a clock with a mini version (continued)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required