C H A P T E R T H R E E
Tables and Trees
A table component was one of the most obvious missing features in AWT,
and among the most welcome additions when Swing came out. However,
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
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