Setting Row Heights and Margins
JTable provides several methods for setting and getting row heights and margins. Table 6 describes the row height and margin methods.
Table 6 JTable Row Height and Margin Methods
Method |
Description |
getIntercellSpacing() |
Returns a java.awt.Dimension object that contains the horizontal and vertical space between cells (measured in pixels). The default spacing is (1, 1), which is sufficient room for grid lines. |
getRowHeight() |
Returns an integer containing the height (measured in pixels) of a table component row. The default height is 16 pixels. |
getRowHeight(int rowIndex) |
Returns an integer containing the height (measured in pixels) of the row identified by rowIndex. The default height is 16 pixels. |
getRowMargin() |
Returns an integer containing the number of pixels rows between table component rows. This method is equivalent to getIntercellSpacing ().height. |
setIntercellSpacing(Dimension ics) |
Sets the height and width (measured in pixels) of a margin between all table component cells to the values contained in the ics referenced object's width and height fields. |
setRowHeight(int rowHeight) |
Sets the height of all rows to the value (measured in pixels) contained in rowHeight. |
setRowHeight(int rowIndex, int rowHeight) |
Sets the height of the row identified by rowIndex to the value (measured in pixels) contained in rowHeight. |
setRowMargin(int rowMargin) |
Sets the number of pixel rows between table component rows (for use as a margin) to the value (measured in pixels) contained in rowMargin. |
You might be confused by the presence of a getRowHeight() method. To what row does that method refer? The answer is any row. When it comes to row height, there are two groups of methods to consider. The first group includes getRowHeight() and setRowHeight(int rowHeight). Collectively, those methods return and set the value of a protected rowHeight field that contains the height of every row. The second group of methods includes getRowHeight(int rowIndex) and setRowHeight(int rowIndex, int rowHeight). Those methods return and set individual row height values. When the table component needs the height of a row, it calls getRowHeight(int rowIndex). If a specific value has not been assigned to the row identified by rowIndex, getRowHeight(int rowIndex) indirectly returns getRowHeight()'s value. (Think of getRowHeight()'s value as a default height.) To see what changes to a row's height (and row margins) do to a table component, take a look at Figure 7.
Figure 7 Individual rows (such as row 1, the second row) can be resized, and all rows can be given margins.
Listing 7 presents source code to a TableDemo7 application that demonstrates calls to the setRowHeight(int rowIndex, int rowHeight) and setRowMargin(int rowMargin) methods.
Listing 7: TableDemo7.java
// TableDemo7.java import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; class TableDemo7 extends JFrame { TableDemo7 (String title) { // Pass the title to the JFrame superclass so that it appears in // the title bar. super (title); // Tell the program to exit when the user either selects Close // from the System menu or presses an appropriate X button on the // title bar. setDefaultCloseOperation (EXIT_ON_CLOSE); // Create a default table model consisting of 4 rows by 2 // columns. DefaultTableModel dtm = new DefaultTableModel (4, 2); // Assign column identifiers (headers) to the columns. String [] columnTitles = { "Name", "Address", }; dtm.setColumnIdentifiers (columnTitles); // Populate all cells in the default table model. String [] names = { "John Doe", "Jane Smith", "Jack Jones", "Paul Finch" }; String [] addresses = { "200 Fox Street", "Apt. 555", "Box 9000", "1888 Apple Avenue" }; int nrows = dtm.getRowCount (); for (int i = 0; i < nrows; i++) { dtm.setValueAt (names [i], i, 0); dtm.setValueAt (addresses [i], i, 1); } // Create a table using the previously created default table // model. JTable jt = new JTable (dtm); // Specify the height of row #1 -- the second row, because rows // indexes are relative to zero -- as twice its size. jt.setRowHeight (1, 2 * jt.getRowHeight (2)); // Leave 5 pixels each on the top and bottom of a row for use as // a margin. jt.setRowMargin (5); // Place the table in a JScrollPane object (to allow the table to // be vertically scrolled and display scrollbars, as necessary). JScrollPane jsp = new JScrollPane (jt); // Add the JScrollPane object to the frame window's content pane. // That allows the table to be displayed within a displayed // scroll pane. getContentPane ().add (jsp); // Establish the overall size of the frame window to 400 // horizontal pixels by 130 vertical pixels. setSize (400, 130); // Display the frame window and all contained // components/containers. setVisible (true); } public static void main (String [] args) { // Create a TableDemo7 object, which creates the GUI. new TableDemo7 ("Table Demo #7"); } }