Java Reference
In-Depth Information
Removing Column Headers
As previously stated, placing a JTable within a JScrollPane automatically produces column
header labels for the different column names. If you don't want column headers, you can
remove them in one of many different ways. Figure 18-5 shows an example of a table without
column headers.
Figure 18-5. JTable without column headers
The simplest way to remove the column headers is to provide empty strings as the column
header names. With the third JTable constructor in the previous list of seven constructors, this
would involve replacing the three column names with "" , the empty string.
Object rowData[][] = {{"Row1-Column1", "Row1-Column2", "Row1-Column3"},
{"Row2-Column1", "Row2-Column2", "Row2-Column3"}};
Object columnNames[] = { "", "", ""};
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
Because this method of removing headers also removes the description of the different
columns, you might want to use another way of hiding column headers. The simplest way is to
just tell the JTable you don't want table headers:
table.setTableHeader(null);
You could also remove headers by subclassing JTable and overriding the protected
configureEnclosingScrollPane() method, or by telling every TableColumn that its header value
is empty. These are more complicated ways of performing the same task.
Note Calling scrollPane.setColumnHeaderView(null) doesn't work to clear out the column
headers. Instead, it causes the JScrollPane to use the default column headers.
JTable Properties
As Table 18-1 shows, the JTable has many properties, 40 in all. These 40 are in addition to the
many properties inherited from the JComponent , Container , and Component classes.
 
Search WWH ::




Custom Search