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

175
Chapter 5
C H A P T E R F I V E
Windows, Dialogs,
and Frames
Hacks 33–40
For four chapters, we’ve hacked away at Swing widgets, from JLabels to
JTables, without worrying too much about the context in which they’re
shown to the user. And yet, every Swing widget must ultimately be con-
tained in some kind of window to be on the screen at all. It’s not an exagger-
ation to say that many competent Swing programmers don’t even know or
care about the hierarchy of AWT Windows, Dialogs, and Frames or their
Swing equivalents,
JWindow, JDialog, and JFrame. Yet, it’s these same pro-
grammers who don’t know that commercial components like splash screens
are all possible in Swing; they see dialogs and frames and assume everything
has a titlebar. This is hardly true, though—you can easily remove the deco-
rations of a dialog, or just work with the window superclass.
Suffice it to say there’s much you can do with windows and their sub-
classes. So much so, in fact, that it fills two chapters. This chapter will deal
with hacks that deal with placing, moving, and resizing windows in ways
that are fairly consistent with the design of the window classes. The next
chapter will be a lot more aggressive in breaking the rules.
H A C K
#33
Window Snapping Hack #33
Make your windows snap to the edges of the screen by using a special event
listener.
Back in the prehistoric days of desktop software, as graphics programs were
being invented, they solved the problem of managing the drawing tools by
creating mini-windows called palettes (and their later variation, toolbars).
Eventually, the programs had so many palettes that the users grew frus-
trated trying to organize them. Lining them up on the edge of the screen was
particularly nasty, so fledgling young programmers took it upon themselves
to create snappable windows. These were windows that were magnetic
176
|
Chapter 5, Windows, Dialogs, and Frames
#33 Window Snapping
HACK
(metaphorically speaking) and could align themselves to the screen’s edges.
This hack demonstrates how to recreate this technique with Java.
The idea is simple: you check whenever the user moves the window. If the
window is off the screen, then move it back to the edge. Moving the win-
dow is pretty easy. The trickier part is knowing when the window has
moved. Fortunately, AWT has an answer: the
ComponentListener
interface.
In Java, every UI component (in both AWT and Swing) fires events when-
ever it moves, resizes, is shown, or is hidden. Any class can receive these
events by implementing the
ComponentListener interface. For the purposes of
this hack, you only need the
componentMoved event, so start by subclassing
ComponentAdapter, which provides default no-operation implementations of
all of
ComponentListener’s declared methods. Then just override the
componentMoved( ) method, as seen in Example 5-1.
Example 5-1. A ComponentListener to snap a window into place
public class WindowSnapper extends ComponentAdapter {
public WindowSnapper( ) { }
private boolean locked = false;
private int snap_distance = 50;
public void componentMoved(ComponentEvent evt) {
if(locked) return;
Dimension size = Toolkit.getDefaultToolkit().getScreenSize( );
int nx = evt.getComponent().getX( );
int ny = evt.getComponent().getY( );
// top
if(ny < 0+snap_distance) {
ny = 0;
}
// left
if(nx < 0+snap_distance) {
nx = 0;
}
// right
if(nx > size.getWidth() - evt.getComponent().getWidth( ) -
snap_distance) {
nx = (int)size.getWidth()-evt.getComponent().getWidth( );
}
// bottom
if(ny > size.getHeight() - evt.getComponent().getHeight( ) -
snap_distance) {
ny = (int)size.getHeight()-evt.getComponent().getHeight( );
}
// make sure we don't get into a recursive loop when the
// set location generates more events

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