Java Reference
In-Depth Information
Listing 18-18. Testing Another Custom Table Cell Editor
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class ChooserTableSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Editable Color Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new ColorTableModel();
JTable table = new JTable(model);
TableColumn column = table.getColumnModel().getColumn(3);
ComboTableCellRenderer renderer = new ComboTableCellRenderer();
column.setCellRenderer(renderer);
TableCellEditor editor = new ColorChooserEditor();
column.setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Printing Tables
One of the newer features with JDK 5.0 is also one of the easiest to use: the ability to print
tables. Through the simple method of public boolean print() throws PrinterException of
JTable , you can print a large table across multiple pages on the printer. It will even spread the
columns across multiple pages if you don't like the default behavior of fitting the table's entire
width on each page.
To demonstrate this behavior, Listing 18-19 takes the basic JTable sample code used to
generate Figure 18-1, adds some more rows to the table, and adds a print button.
Search WWH ::




Custom Search