Now press the mouse button and drag to the right or left. The column changes size, and the other columns adjust to fill the remaining space. Resize the window containing the table so that it's bigger than necessary to display the whole table.
All the table cells become wider, expanding to fill the extra horizontal space. The table in. Simple. Table. Demo. java declares the column names in a String array. String. However, these constructors also have disadvantages: They automatically make every cell editable. They treat all data types the same (as strings). For example, if a table column has Boolean data, the table can display the data in a check box.
However, if you use either of the two JTable constructors listed previously, your Boolean data is displayed as a string. You can see this difference in the Vegetarian column of the previous figure. They require that you put all of the table's data in an array or vector, which may not be appropriate for some data. For example, if you are instantiating a set of objects from a database, you might want to query the objects directly for their values, rather than copying all their values into an array or vector. If you want to get around these restrictions, you need to implement your own table model, as described in Creating a Table Model. Here is typical code for creating a scroll pane that serves as a container for a table. JScroll. Pane scroll.
Pane = new JScroll. Pane(table). table.
Fills. Viewport. Height(true). The two lines in this snippet do the following: The JScroll. Pane constructor is invoked with an argument that refers to the table object. This creates a scroll pane as a container for the table; the table is automatically added to the container. JTable. set. Fills.
Viewport. Height is invoked to set the fills. Viewport. Height property. When this property is true the table uses the entire height of the container, even if the table doesn't have enough rows to use the whole vertical space. This makes it easier to use the table as a drag- and- drop target. The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled. If you are using a table without a scroll pane, then you must get the table header component and place it yourself.
For example. container. Layout(new Border. Layout()). container. Table. Header(), Border.
Layout. PAGE. When the table becomes wider or narrower (which might happen when the user resizes the window containing the table), all the column widths change appropriately. When the user resizes a column by dragging its right border, then either other columns must change size, or the table's size must change. By default, the table's size remains the same, and all columns to the right of the drag point resize to accommodate space added to or removed from the column to the left of the drag point. To customize initial column widths, you can invoke set. Preferred. Width on each of your table's columns.
This sets both the preferred widths of the columns and their approximate relative widths. For example, adding the following code to Simple.
Table. Demo makes its third column bigger than the other columns. Table. Column column = null.
Table. Column supplies getter and setter methods for the minimum, preferred, and maximum widths of a column, as well as a method for getting the current width. For an example of setting cell widths based on an approximation of the space needed to draw the cells' contents, see the init. Column. Sizes method in. Table. Render. Demo.
When the user explicitly resizes columns, the columns' preferred widths are set such that the user- specified sizes become the columns' new current widths. However, when table itself is resized — typically because the window has resized —; the columns' preferred widths do not change.
Instead, the existing preferred widths are used to calculate new column widths to fill the available space. You can change a table's resize behavior by invoking. Auto. Resize. Mode.
In its default configuration, a table supports a selection that consists of one or more rows. The user can select a contiguous range of rows or an arbitrary set of rows. The last cell that the user indicated gets a special indication; in the Metal look and feel, the cell is outlined.
This cell is known as the lead selection; it is sometimes called . Or, to compile and run the example yourself, consult the example index. This example program presents the familiar table, and allows the user to manipulate certain JTable options. There is also a text pane that logs selection events.
In the screenshot below, a user has run the program, clicked in the first row, then control- clicked in the third row. Notice the outline around the last cell clicked; this is how the Metal look and feel highlights the lead selection. Under . Click the one labelled . Now you can only select one row at a time. If you click on the . This method takes a single argument, which must be one of the following constants defined in javax. List. Selection. Model: MULTIPLE.
When this bound property is true (and the column. Selection. Allowed property is false), the user can select by row. When this bound property is true (and the row.
Selection. Allowed bound property is false), the user can select by column. When this bound property is true, the user can select a single cell or rectangular block of cells. NOTE: JTable uses a very simple concept of selection, managed as an intersection of rows and columns. It was not designed to handle fully independent cell selections. This is because cell selection is not supported in this mode in the demo. You can specify selection by cell in multiple interval selection mode, but the result is a table that does not produce useful selections.
You may also notice that changing any of the three selection options can affect the others. This is because allowing both row selection and column selection is exactly the same as enabling cell selection. JTable automatically updates the three bound variables as necessary to keep them consistent.
NOTE: Setting cell. Selection. Enabled to a value has the side effect of also setting both row. Selection. Enabled and column. Selection. Enabled to that value. Setting both row.
Selection. Enabled and column. Selection. Enabled to a value has the side effect of also setting cell. Selection. Enabled to that value.
Setting row. Selection. Enabled and column. Selection. Enabled to different values has the side effect of also setting cell. Selection. Enabled to false. To retrieve the coordinates of the lead selection, refer to the selection models for the table itself and for the table's column model.
The following code formats a string containing the row and column of the lead selection. String. format(. For information on these, refer to. How to Write a List Selection Listener in the. Writing Event Listeners lesson.
NOTE: Selection data actually describes selected cells in the . This distinction does not matter unless your viewed data has been rearranged by sorting, filtering, or user manipulation of columns. Installing And Configuring Oracle Identity Manager Tutorials here. In that case, you must convert selection coordinates using the conversion methods described in Sorting and Filtering. A table model object must implement the.