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

240
|
Chapter 6, Transparent and Animated Windows
#46 Slide Notes Out from the Taskbar
HACK
H A C K
#46
Slide Notes Out from the Taskbar Hack #46
Pop up a note above the taskbar when your application wants attention.
On Windows, long-running applications sometimes will slide in a window
above the taskbar to call attention to themselves when an interesting event
occurs, such as a finished download or an IM buddy’s appearance.
If you want to do this in Java, you need to deal with a pretty significant
problem: neither AWT nor Swing has any concept of the taskbar (where it
is, how big it is, whether it’s auto-hiding, or anything else). As a result, you
don’t know where to draw the window, and just taking a guess or hardcod-
ing something is hazardous—too high and the window floats inexplicably
on the desktop, too low and it gets buried under the taskbar.
Furthermore, how is this going to work on other operating systems? On the
Mac, the proper way to get attention is to bounce your application’s dock
icon. Since there’s no API exposing that functionality, can you at least use a
Windows-like slide-in window above the dock? Sure…if you can figure out
how tall the dock is (it’s user configurable), or whether the dock is even on
the bottom of the screen (it might be on the right or left, too).
Fortunately, it is possible to figure out what unobstructed space is available
to you on the main display. After that, it’s just a matter of offscreen imaging
and animation.
Figure Out Where You Are
The key to figuring out your available space is to get the local
GraphicsEnvironment, which describes the display, and then call
getMaximumWindowBounds( ). This method, introduced in Java 1.4, returns a
Rectangle representing the largest centered Window that could fit on the dis-
play, accounting for objects that intrude on the display’s usable space, like
the Windows taskbar or the Mac’s monolithic menu bar.
Figure 6-9. Successive screenshots of an animated glass pane sheet
Slide Notes Out from the Taskbar #46
Chapter 6, Transparent and Animated Windows
|
241
HACK
This means that on Windows, the Rectangle will have an upper-left corner
at
0,0; on the Mac, it will be at 0,22, which leaves space for the Mac’s menu
bar. Meanwhile, the height of the
Rectangle
won’t be the height of your dis-
play unless you have your taskbar set to auto-hide, or you have moved it to
the right or left.
So, now you have the beginnings of the slide-in above-taskbar window. By
subtracting the height of the window from the y-coordinate of the last
usable row, you can place the window directly above the taskbar or dock.
To do the slide-in, you’ll need to do an animation loop in which progres-
sively larger portions of the complete window are blitted into a smaller
onscreen version. This is very similar to the animated sheet
[Hack #45] you’ve
already seen. It’s so similar, in fact, that you can reuse the inner class from
that hack to do the progressive redrawing.
A simple implementation,
SlideInNotification, is shown in Example 6-11.
Example 6-11. Sliding in a window immediately above the taskbar or dock
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class SlideInNotification extends Object {
protected static final int ANIMATION_TIME = 500;
protected static final float ANIMATION_TIME_F =
(float) ANIMATION_TIME;
protected static final int ANIMATION_DELAY = 50;
JWindow window;
JComponent contents;
AnimatingSheet animatingSheet;
Rectangle desktopBounds;
Dimension tempWindowSize;
Timer animationTimer;
int showX, startY;
long animationStart;
public SlideInNotification ( ) {
initDesktopBounds( );
}
public SlideInNotification (JComponent contents) {
this( );
setContents (contents);
}

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