102
Chapter 3
C H A P T E R T H R E E
Tables and Trees
Hacks 21–27
A table component was one of the most obvious missing features in AWT,
and among the most welcome additions when Swing came out. However,
the
JTable may be used too much—it’s easy to throw an Object[][] at the
constructor and get a full-blown GUI table, and some developers don’t
question the wisdom of this sort of coding.
But despite the generosity of the Swing
JTable API, there are a few things
still missing. Wouldn’t it be nice if the table model keep itself sorted, or if
the column widths had a non-ugly default that takes their contents into
account? Well, you didn’t buy this book to argue API theory—the point
here is to hack things into shape. So, let’s get started.
H A C K
#21
Size Your Columns to Suit Your JTable’s
Contents Hack #21
A one-digit column does not need 100 pixels of dead space. You know this;
your JTables should, too.
Does Figure 3-1 look like your typical JTable?
Figure 3-1. JTable with default column sizing

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.