Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click here for Oracle 8i Internet Seminars.
Click here for Oracle 8i Internet Seminars.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The previous paragraph explained how to set the selection mode for a JTable instance. After the mode is set, it may be necessary to set or query the selection set in the table. The methods presented in Chapter 10 for the ListSelectionModel can be used for setting and getting the set of selected rows or columns from the row or column list selection model. The JTable class also provides convenience functions for many of these methods. This allows the selected rows and columns to be queried directly from the table.

The setRowSelectionInterval can be used to select rows in an inclusive range defined by the two integer parameters passed to the method. Rows can be inserted into the selection set by using addRowSelectionInterval. This method also takes two parameters to define the inclusive range of rows to add to the selected row set. The selection mode must support the rows added to the set or all rows cannot be selected.

The setColumnSelectionInterval and addColumnSelectionInterval methods provide analogous functionality for columns. Rows or columns can be removed from the selection set with the removeRowSelectionInterval and the removeColumnSelectionInterval methods. Both these methods also take two integer parameters that define the inclusive range of rows or columns to be removed from the selection set. To remove a single row from the selected set, both parameters would be the same: the row to be deselected.

The clearSelection method clears both the row and column selection set. The selectAll method checks the current selection, and, if a row is selected, all rows are selected. Also, if a column is selected, all columns are selected. If both are selected when this method is called, all rows and columns are selected.

The number of selected rows can be determined with the getSelectedRowCount method. The getSelectedRows method returns an array of the indices of all selected rows or an empty array if there are not any selected rows. The getSelectedRow method returns the index of the last selected row, or –1 if no rows are selected. The equivalent methods for querying column selection status are getSelectedColumnCount, getSelectedColumns, and getSelectedColumn. The table provides the isRowSelected, isColumnSelected, and isCellSelected method to easily test if a row, column, or cell is selected.

The ListSelectionModel for rows or columns can be set directly. This allows for custom selection models, or, as was shown in Chapter 10 for the JList class, for sharing selection models. A ListSelectionModel can be shared between lists and tables. For rows, the ListSelectionModel is set by using the setSelectionModel method. To set the selection model for columns, the column model must first be obtained from the table, and then the selection model set on the column model. This can be done with the following code fragment:

ListSelectionModel mySelectionModel;
// mySelectionModel created and initialized.
table.getColumnModel().setSelectionModel( mySelectionModel );

Column Creation and Resizing Modes

You can configure JTable instances to automatically create columns whenever its data model is set or changes structure. Passing true to the setAutoCreateColumnsFromModel method enables this capability. The columns are created in the createDefault ColumnsFromModel method. This method creates an instance of the TableColumn class for each column in the model and adds it to the table. The TableColumn class is discussed further in the next section. The current value of this property can be queried by using the getAutoCreateColumnsFromModel method. If this property is false, it is the table’s client’s responsibility to create and add columns to the table. createDefaultColumnsFromModel is a public method that can be called by clients.

Column headers allow columns to be resized by dragging on the border between two columns. (Columns can be configured to not be resizable, or have minimum and maximum sizes. This will be shown in the next section.) As the user drags to set a column’s width, the behavior of other columns is defined by the autoResizeMode property. The setAutoResizeMode sets the property to one of the three legal values shown in Table 12.2. These values are integer constants defined in the JTable class.

Table 12.2 Column Resize Modes

Mode Constant Behavior

AUTO_RESIZE_OFF Do nothing
AUTO_RESIZE_LAST_COLUMN Space goes to last column
AUTO_RESIZE_ALL_COLUMNS Space distributed between all columns

When the autoResizeMode property is set to AUTO_RESIZE_OFF when a column is resized, the other columns move to allow the column to expand or contract, but do not change size. In this mode, the width of the table changes as the column size changes. When the mode is AUTO_RESIZE_LAST_COLUMN, the last column expands or contracts an amount equal to the space removed or added to the column being resized. When the mode is AUTO_RESIZE_ALL_COLUMNS, space given or taken from the column being resized is evenly distributed to all other columns in the table. The last two modes do not change the width of the table as a column is resized. The default mode for this property is AUTO_RESIZE_ALL_COLUMNS.

You can specify the space between rows and columns with the setIntercellSpacing method. This method takes a Dimension as its single parameter. As such, the row spacing can be specified using the Dimension’s width property and the column spacing with its height property. The current spacing can be queried by using the getInterCellSpacing. The width and height of the returned dimension are set to the row and column spacing, respectively.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.