Java Reference
In-Depth Information
Figure 16.3 A static table
It is interpreted as an array consisting of the rows, i.e. content[i][] is the one-
dimensional array containing the entries of the i th row of the table. In a second,
one-dimensional array columnNames the headers of the columns are passed. The
rows and the array with the column names all have to have the same length! The
constructor then looks like this:
JTable(Object[][] content,Object[] columnNames)
Note that the arrays are of type Object .If'non-standard' types are used, one
has to ensure that the program knows how to render them by supplying a user-
defined renderer. This is done by implementing the interface TableCellRenderer .
By default, JTable uses a DefaultTableCellRenderer which can display the
'standard' types such as strings, images and boolean values. For most purposes
this is sufficient.
!
In order to make the column names visible the table should
be embedded into a scroll pane. The heads remain visible even if the table is
vertically scrolled. By default, the widths of the columns are all the same and they
are automatically adjusted such that all columns fit into the visible area. This is
done even though a scroll pane is used to display the table. If the visible area
is small then the columns become too narrow to properly display the content of
some or all the cells. In those cells some dots ( ... ) are shown instead. To switch
the automatic width adjustment off one uses the command:
setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
The following listing StaticTableFrame is an example for creating and displaying
a table. The resulting display is shown in Figure 16.3.
File: its/Tables/StaticTableFrame.java
1. package its.Tables;
2.
3. import its.SimpleFrame.SimpleFrame;
4. import javax.swing.JScrollPane;
5. import javax.swing.JTable;
Search WWH ::




Custom Search