500
|
Chapter 12, Miscellany
#100 Make Frames Resize Dynamically
HACK
ShellFolder is a wrapper for metadata of the selected file. With this object,
you can retrieve both the icon and a text description of the file’s type. A nor-
mal MP3 icon would be only 16 × 16 pixels (Figure 12-15), but if you ran the
MP3 file through
LargeIconText
, it would print the string type = MPEG
Layer 3 Audio and show a much nicer 32 × 32 pixel media icon
(Figure 12-16).
H A C K
#100
Make Frames Resize Dynamically Hack #100
Make your application feel more responsive by turning on dynamic layout.
By default, JFrames don’t resize dynamically. This means that the frame will
not redraw itself as the user is resizing it. A repaint will only occur after the
user lets go of the mouse and the window is refreshed. This behavior often
results in extra gray areas and an unresponsive-feeling application. How-
ever, you can fix this with just one method call!
Just call one method on the default
Toolkit:
Toolkit.getDefaultToolkit( ).setDynamicLayout(true);
You can query the dynamic layout property like this:
if(Toolkit.getDefaultToolkit().isDynamicLayoutActive( )) {
// do something
}
or like this:
if(Toolkit.getDefaultToolkit().isDynamicLayoutSet( )) {
// do something
}
isDynamicLayoutSet( ) will tell you if dynamic layout was set programmati-
cally, while
isDynamicLayoutActive( ) will tell you if dynamic layout is sup-
ported. You need to use both methods because some platforms don’t
support dynamic layout, and others don’t let you turn it off.
Figure 12-15. Normal MP3 icon
Figure 12-16. Large MP3 icon

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.