O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

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

Chapter 16. Advanced Table Examples

In this chapter, we’re going to take a different approach. Tables are extremely flexible, useful gadgets. Here, we’re going to show you how to put tables to work in more advanced situations. Most of these require working on the TableModel itself or the TableColumnModel. But once you know what you’re doing, subclassing these models is fairly easy and gives you a lot of flexibility.

We’re going to look at three particular examples:

  • A scrollable table with row headers. Remember that a JTable understands column headers, but doesn’t have any concept of a row header. Also, remember that a JScrollPane understands both column and row headers. In this example, we’ll show you how to add row headers to a JTable and make them work properly within a JScrollPane.

  • A table that has an extremely large numbers of rows. Scrolling stops working well when you have more than a few hundred rows to work with. We’ll build a table with 10,000 rows, let you page up and down to select a range of 100 rows within the table, and then scroll back and forth within that more limited range.

  • A TableChart component that builds pie charts based on the TableModel class used by JTable. In this example, the JTable is almost superfluous, although it provides a convenient way to edit the data in the pie chart. The real point is that the TableModel is a powerful abstraction that can be put to use even when there’s no table around.

A Table with Row Headers

As we promised, this is a table ...

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