
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 ...