Java Reference
In-Depth Information
Example 10•18: FontChooser.java (continued)
// If not cancelled, set the button font
if (font != null) button.setFont(font);
}
});
// Display the demo
frame.getContentPane().add(button);
frame.setSize(200, 100);
frame.show();
}
}
}
Displaying Tables
Now that we've seen how to assemble a prototypical Swing GUI, we can move on
and start studying some more advanced Swing programming topics. We'll start
with examples of some of the more powerful, and therefore complicated, compo-
nents. The JTable class displays tabular data. It is particularly easy to use if your
data happens to be organized into arrays of arrays. If this is not the case, however,
you must implement the javax.swing.table.TableModel interface to serve as a
translator between your data and the JTable component.
Example 10-19, which is a listing of Pr opertyTable.java , does exactly this. Proper-
tyTable is a subclass of JTable that uses a custom TableModel implementation to
display a table of the properties defined by a specified JavaBeans class. The exam-
ple includes a main() method so you can run it as a standalone application. Figure
10-15 shows the PropertyTable class in action. When studying this example, pay
particular attention to the TableModel implementation; the TableModel is the key
to working with the JTable component. Also note the PropertyTable constructor
method that uses the TableColumnModel to modify the default appearance of the
columns in the table.
Figur e 10•15. The Pr opertyTable application
Example 10•19: PropertyTable.java
package com.davidflanagan.examples.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
// TableModel and other JTable-related classes
Search WWH ::




Custom Search