228
|
Chapter 6, Transparent and Animated Windows
#44 Turn Dialogs into Frame-Anchored Sheets
HACK
H A C K
#44
Turn Dialogs into Frame-Anchored Sheets Hack #44
One of Mac OS X’s best ideas is binding the dialog to the window it blocks.
This hack shows you how to mimic this in Swing.
One of my favorite features in Mac OS X is the sheet. This is a dialog box
replacement that slides down from a window’s titlebar. Figure 6-7 shows an
example of a sheet in Apple’s Safari web browser.
Why Sheets Rock
Looking at it, you might think, “what’s the big deal” or “how is this any dif-
ferent than a regular dialog?” Oh, it’s far better:
A sheet is visually anchored to the window that it blocks
On platforms where dialogs have titlebars and close boxes, the relation-
ship between a dialog and the window it blocks is not necessarily intui-
tive. On a related point….
Example 6-6. Testing out custom tool tips
public class ToolTipsHack {
public static void main(String[] args) {
JButton button;
JFrame frame = new JFrame("Tool Tips Hack");
BoxLayout layout = new BoxLayout(
frame.getContentPane( ),
BoxLayout.Y_AXIS);
frame.getContentPane( ).setLayout(layout);
button = new CustomJButton( );
button.setText("Open");
button.setToolTipText("Open an existing file");
frame.getContentPane( ).add(button);
button = new CustomJButton( );
button.setText("Save");
button.setToolTipText("Save the currently open file");
frame.getContentPane( ).add(button);
frame.getContentPane( ).add(new JLabel("a label"));
frame.getContentPane( ).add(new JLabel("a label"));
frame.getContentPane( ).add(new JLabel("a label"));
frame.pack( );
frame.show( );
}
}

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.