13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16.7 Getting Down to Cases: Designing a GUI for BudgetPro35916.7.2.7 JTable: The Workhorse of Data DisplayLook again at our GUI applicati<strong>on</strong>. In its center you see the table object:This is a JTable. A simple way to create a JTable is by passing in twoarrays to the c<strong>on</strong>structor—first, a two-dimensi<strong>on</strong>al array of data objects, andsec<strong>on</strong>d, a <strong>on</strong>e-dimensi<strong>on</strong>al array of column names. Notice that we said dataobjects; you need to use Integer objects, not simple int types, and Doublesinstead of doubles. This allows the c<strong>on</strong>structor to take any Object type anddisplay it in the table via the object’s toString() method.While this form of a table is simple to use, it usually isn’t enough for allthe various things you’ll want to do with a table. Let’s look at the “industrialstrength” table initializati<strong>on</strong>. For that, we need to talk about a table model.16.7.2.8 Table ModelIf you’ve ever taken an object-oriented design class, they’ve probably talkedabout the Model/View/C<strong>on</strong>troller design pattern. (If you haven’t taken such aclass, at least read a good book or two <strong>on</strong> the subject; it will improve your Javaprogramming skills.) A simpler versi<strong>on</strong> of this pattern is the View/Model pattern.What it describes is separating the core of the data from the frill of itspresentati<strong>on</strong>—what you want to display versus how you want to display it. TheModel is the underlying data; the View is <strong>on</strong>e particular way to show that data.This View versus Model distincti<strong>on</strong> is used to great effect with JTableand TableModel objects in Swing. What you need to do is create aTableModel, then give that TableModel to the JTable via the JTable’sc<strong>on</strong>structor. The TableModel will give you all sorts of c<strong>on</strong>trol over yourdata—how, where, and when to get or update it. The JTable will display itand let you rearrange or resize the columns.Rather than implement a complete TableModel from scratch, Swing givesus a helping hand with its AbstractTableModel class. AbstractTableModelis a partially implemented class which handles most of the grundy details—it

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!