The JavaTM Tutorial
Start of Tutorial > Start of Trail > Start of Lesson

Trail: Creating a GUI with JFC/Swing
Lesson: Using Swing Components How to Use Tables With the JTable

[PENDING: Update this snapshot. Label these parts: - each cell displays an item of data - each column header describes its column maybe also have these labels: - each column contains one type of data - a table header displays the column headers]
The rest of this section tells you how to accomplish some common table-related tasks. Here are the topics this section covers: Creating a Simple Table Adding a Table to a Container Setting and Changing Column Widths Detecting User Selections Creating a Table Model Detecting Data Changes Concepts: Editors and Renderers Using a Combo Box as an Editor Using an Editor to Validate User-Entered Text Using Other Editors Using Custom Renderers Specifying Tool Tips for Cells Specifying Tool Tips for Column Headers Sorting and Otherwise Manipulating Data The Table API Examples that Use Tables Creating a Simple Table

[PENDING: This figure will be updated.] Try this: Run SimpleTableDemo using JavaTM Web Start
The entire first row is selected, indicating that you have selected Mary Campione's data. A special highlight indicates that the "Snowboarding" cell is editable. Generally, you begin editing a text cell by double-clicking it. Position the cursor over "First Name". Now press the mouse button and drag to the right.
As you can see, users can rearrange columns in tables. Position the cursor just to the right of a column header. 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. Here is the code that implements the table in SimpleTableDemo.java
The SimpleTableDemo example uses one of two JTable constructors that directly accept data: JTable(Object[][] rowData, Object[] columnNames) JTable(Vector rowData, Vector columnNames) The advantage of these constructors is that they're easy to use. 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 one of the two JTable constructors listed previously, your Boolean data will be displayed as a string. You can see this difference in the last column of the two previous pictures of tables. They require that you put all of the table's data in an array or vector, which isn't appropriate for some data. For example, if you're 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. Adding a Table to a Container It's easy to put a table in a scroll pane. You need just one or two lines of code: JScrollPane scrollPane = new JScrollPane(table); table.setPreferredScrollableViewportSize(new Dimension(500, 70));
The scroll pane automatically gets the table's header, which displays the column names, and puts it on top of the table. Even when the user scrolls down, the column names remain visible at the top of the viewing area. The scroll pane also tries to make its viewing area the same as the table's preferred viewing size. The previous code snippet sets the table's preferred viewing size with the setPreferredScrollableViewportSize method. If you're using a table without a scroll pane, then you must get the table header component and place it yourself. For example: container.setLayout(new BorderLayout()); container.add(table.getTableHeader(), BorderLayout.PAGE_START); container.add(table, BorderLayout.CENTER);
Setting and Changing Column Widths By default, all columns in a table start out with equal width, and the columns automatically fill the entire width of the table. 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. The following figures illustrate the default resizing behavior. [PENDING: These will be updated.]

Initially, the columns have equal width.

When the user resizes a column, some of the other columns must adjust size for the table to stay the same size.

When the entire table is resized, all the columns are resized.
To customize initial column widths, you can invoke setPreferredWidth 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 SimpleTableDemo makes its third column bigger than the other columns: TableColumn column = null; for (int i = 0; i < 5; i++) { column = table.getColumnModel().getColumn(i); if (i == 2) { column.setPreferredWidth(100); //sport column is bigger } else { column.setPreferredWidth(50); } }
As the preceding code shows, each column in a table is represented by a TableColumn
Detecting User Selections The following code snippet shows how to detect when the user selects a table row. By default, a table allows the user to select multiple rows — not columns or individual cells — and the selected rows need not be next to each other. Using the setSelectionMode method, the following code specifies that only one row at a time can be selected. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ... //Ask to be notified of selection changes. ListSelectionModel rowSM = table.getSelectionModel(); rowSM.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel)e.getSource(); if (lsm.isSelectionEmpty()) { ...//no rows are selected } else { int selectedRow = lsm.getMinSelectionIndex(); ...//selectedRow is selected } } });
The code is from SimpleTableSelectionDemo.java
Creating a Table Model As the following figure shows, every table gets its data from an object that implements the TableModel

[PENDING: This figure might be updated to look more modern.] The JTable constructor used by SimpleTableDemo creates its table model with code like this: new AbstractTableModel() { public String getColumnName(int col) { return columnNames[col].toString(); } public int getRowCount() { return rowData.length; } public int getColumnCount() { return columnNames.length; } public Object getValueAt(int row, int col) { return rowData[row][col]; } public boolean isCellEditable(int row, int col) { return true; } public void setValueAt(Object value, int row, int col) { rowData[row][col] = value; fireTableCellUpdated(row, col); } }
As the preceding code shows, implementing a table model can be simple. Generally, you implement your table model in a subclass of the AbstractTableModel

[PENDING: This figure will be updated.] This table is different from the SimpleTableDemo table in the following ways: TableDemo's custom table model, even though it's simple, can easily determine the data's type, helping the JTable display the data in the best format. SimpleTableDemo's automatically created table model, on the other hand, isn't smart enough to know that the # of Years column contains numbers (which should generally be right aligned and have a particular format). It also doesn't know that the Vegetarian column contains boolean values, which can be represented by check boxes. In TableDemo, we implemented the custom table model so that it doesn't let you edit the name columns; it does, however, let you edit the other columns. In SimpleTableDemo, all cells are editable. Below is the code from TableDemo.java
Detecting Data Changes A table and its model automatically detect whenever the user edits the table's data. However, if the data changes for another reason, you must take special steps to notify the table and its model of the data change. Also, if you don't implement a table model, as in SimpleTableDemo, then you must take special steps to find out when the user edits the table's data. An example of updating a table's data without directly editing it is in the BINGO application. The BINGO application, which is presented in BINGO!
Concepts: Editors and Renderers Before you go on to the next few tasks, you need to understand how tables draw their cells. You might expect each cell in a table to be a component. However, for performance reasons, Swing tables aren't implemented that way. Instead, a single cell renderer is generally used to draw all of the cells that contain the same type of data. You can think of the renderer as a configurable ink stamp that the table uses to stamp appropriately formatted data onto each cell. When the user starts to edit a cell's data, a cell editor takes over the cell, controlling the cell's editing behavior. For example, each cell in the # of Years column in TableDemo contains Number data — specifically, an Integer object. By default, the cell renderer for a Number-containing column uses a single JLabel instance to draw the appropriate numbers, right-aligned, on the column's cells. If the user begins editing one of the cells, the default cell editor uses a right-aligned JTextField to control the cell editing. To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. (We'll tell you how to specify renderers a bit later.) If you didn't, then the table invokes the table model's getColumnClass method, which gets the data type of the column's cells. Next, the table compares the column's data type with a list of data types for which cell renderers are registered. This list is initialized by the table, but you can add to it or change it. Currently, tables put the following types of data in the list: Boolean — rendered with a check box. Number — rendered by a right-aligned label. Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat
Use the cell editor (or register a listener on the cell editor).
To detect row/column/cell selections and deselections...
Use a selection listener as described in Detecting User Selections.
To detect mouse events on a column header...
Register the appropriate type of mouse listener
To detect other events...
Register the appropriate listener on the JTable object.
The next few sections tell you how to customize display and editing by specifying renderers and editors. You can specify cell renderers and editors either by column or by data type.
Using a Combo Box as an Editor Setting up a combo box as an editor is simple, as the following example shows. The bold line of code sets up the combo box as the editor for a specific column. TableColumn sportColumn = table.getColumnModel().getColumn(2); ... JComboBox comboBox = new JComboBox(); comboBox.addItem("Snowboarding"); comboBox.addItem("Rowing"); comboBox.addItem("Chasing toddlers"); comboBox.addItem("Speed reading"); comboBox.addItem("Teaching high school"); comboBox.addItem("None"); sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
Here is a picture of the combo box editor in use:

[PENDING: This figure will be updated. It should probably include the cursor.] The preceding code is from TableRenderDemo.java
Using an Editor to Validate User-Entered Text If a cell's default editor allows text entry, you get some error checking for free if the cell's type is specified as something other than String or Object. The error checking is a side effect of converting the entered text into an object of the proper type. Version note: Before 1.3, the default implementation did not in any way restrict the string that could be entered, and didn't convert it from a String. You needed to put some ugly code in the model's setValueAt method to parse the entered string and prevent the cell's value from becoming a String. The automatic checking of user-entered strings occurs when the default editor attempts to create a new instance of the class associated with the cell's column. The default editor creates this instance using a constructor that takes a String as an argument. For example, in a column whose cells have type Integer, when the user types in "123" the default editor creates the corresponding Integer using code equivalent to new Integer("123"). If the constructor throws an exception, the cell's outline turns red and refuses to let focus move out of the cell. If you implement a class used as a column data type, you can use the default editor if your class supplies a constructor that takes a single argument of type String. If you like having a text field as the editor for a cell, but want to customize it — perhaps to check user-entered text more strictly or to react differently when the text is invalid — you can change the cell editor to use a formatted text field. The formatted text field can check the value either continuously while the user is typing or after the user has indicated the end of typing (such as by pressing Enter). The following code, taken from a demo named TableFTFEditDemo, sets up a formatted text field as an editor that limits all integer values to be between 0 and 100. You can run TableFTFEditDemo using Java Web Start
The IntegerEditor class is implemented as a subclass of DefaultCellEditor
Using Other Editors Whether you're setting the editor for a single column of cells (using the TableColumn setCellEditor method) or for a specific type of data (using the JTable setDefaultEditor method), you specify the editor using an argument that adheres to the TableCellEditor interface. Fortunately, the DefaultCellEditor class implements this interface and provides constructors to let you specify an editing component that's a JTextField, JCheckBox, or JComboBox. You usually don't have to explicitly specify a check box as an editor, since columns with Boolean data automatically use a check box renderer and editor. What if you want to specify an editor that isn't a text field, check box, or combo box? Well, because DefaultCellEditor doesn't support other types of components, you must do a little more work. You need to create a class that implements the TableCellEditor



As you can see, the code is pretty simple. The only part that's a bit tricky is the call to fireEditingStopped at the end of the editor button's action handler. Without this call, the editor would remain active, even though the modal dialog is no longer visible. The call to fireEditingStopped lets the table know that it can deactivate the editor, letting the cell be handled by the renderer again. Using Custom Renderers This section tells you how to create and specify a cell renderer. You can set a type-specific cell renderer using the JTable method setDefaultRenderer. To specify that cells in a particular column should use a renderer, you use the TableColumn method setCellRenderer. You can even specify a cell-specific renderer by creating a JTable subclass, as we'll show later. It's easy to customize the text or image rendered by the default renderer, DefaultTableCellRenderer. You just create a subclass and implement the setValue method so that it invokes setText or setIcon with the appropriate string or image. For example, here is how the default date renderer is implemented: static class DateRenderer extends DefaultTableCellRenderer { DateFormat formatter; public DateRenderer() { super(); } public void setValue(Object value) { if (formatter==null) { formatter = DateFormat.getDateInstance(); } setText((value == null) ? "" : formatter.format(value)); } }
If extending DefaultTableCellRenderer doesn't do the trick, you can build a renderer using another superclass. The easiest way is to create a subclass of an existing component, making your subclass implement the TableCellRenderer
Here is the code from TableDialogEditDemo.java
The next section shows a couple of examples of using TableColumn's setCellRenderer method, so we'll skip that for now and show you how to specify a renderer for a particular cell. To specify a cell-specific renderer, you need to define a JTable subclass that overrides the getCellRenderer method. For example, the following code makes the first cell in the first column of the table use a custom renderer: TableCellRenderer weirdRenderer = new WeirdRenderer(); table = new JTable(...) { public TableCellRenderer getCellRenderer(int row, int column) { if ((row == 0) && (column == 0)) { return weirdRenderer; } // else... return super.getCellRenderer(row, column); } };
Specifying Tool Tips for Cells By default, the tool tip text displayed for a table cell is determined by the cell's renderer. However, sometimes it can be simpler to specify tool tip text by overriding JTable's implementation of the getToolTipText(MouseEvent) method. This section tells you how to use both techniques. To add a tool tip to a cell using its renderer, you first need to get or create the cell renderer. Then, after making sure the rendering component is a JComponent, invoke the setToolTipText method on it. An example of setting tool tips for cells is in TableRenderDemo, which you can run using Java Web Start
Here is a picture of the resulting tool tip: [PENDING: We will put a picture here. It might be cropped to just include the relevant cell and its tool tip (along with whatever other cells are between them).] Although the tool tip text in the previous example is static, you can also implement tool tips whose text changes depending on the state of the cell or program. Here are a couple ways to do so: Add a bit of code to the renderer's implementation of the getTableCellRendererComponent method. Override the JTable method getToolTipText(MouseEvent). An example of adding code to a cell renderer is in TableDialogEditDemo (which you can run using Java Web Start
Here is an example of what the tool tip looks like:

[PENDING: this snapshot needs to be updated. It should probably be cropped, as well.] As we mentioned before, you can specify tool tip text by overriding JTable's getToolTipText(MouseEvent) method. The program TableToolTipsDemo shows how. You can run TableToolTipsDemo using Java Web Start

[PENDING: this snapshot needs to be updated and cropped.] Here is the code from TableToolTipsDemo.java
The code is fairly straightforward, except perhaps for the call to convertColumnIndexToModel. That call is necessary because if the user moves the columns around, the view's index for the column doesn't match the model's index for the column. For example, the user might drag the Vegetarian column (which the model considers to be at index 4) so it's displayed as the first column — at view index 0. Since prepareRenderer gives us the view index, we need to translate the view index to a model index so we can be sure we're dealing with the intended column. Specifying Tool Tips for Column Headers You can add a tool tip to a column header by setting the tool tip text for the table's JTableHeader. Often, different column headers require different tool tip text. You can change the text by overriding the table header's getToolTipText(MouseEvent) method. An example of using the same tool tip text for all column headers is in TableSorterDemo. Here is how it sets the tool tip text: table.getTableHeader().setToolTipText( "Click to sort; Shift-Click to sort in reverse order");
TableToolTipsDemo has an example of implementing column header tool tips that vary by column. If you run TableToolTipsDemo, which you can do using Java Web Start

[PENDING: this snapshot needs to be updated. It should also be cropped.] The following code implements the tool tips. Basically, it creates a subclass of JTableHeader that overrides the getToolTipText(MouseEvent) method so that it returns the text for the current column. To associate the revised table header with the table, the JTable method createDefaultTableHeader is overridden so that it returns an instance of the JTableHeader subclass. protected String[] columnToolTips = { null, null, "The person's favorite sport to participate in", "The number of years the person has played the sport", "If checked, the person eats no meat"}; ... JTable table = new JTable(new MyTableModel()) { ... //Implement table header tool tips. protected JTableHeader createDefaultTableHeader() { return new JTableHeader(columnModel) { public String getToolTipText(MouseEvent e) { String tip = null; java.awt.Point p = e.getPoint(); int index = columnModel.getColumnIndexAtX(p.x); int realIndex = columnModel.getColumn(index).getModelIndex(); return columnToolTips[realIndex]; } }; } };
Version note: Before 1.3, each column had its own header renderer, and you could use the value returned by the TableColumn method getHeaderRenderer to set a tool tip for a specific column header. For performance reasons, the default behavior is now to use a single renderer for all column headers, and getHeaderRenderer returns null. The default header renderer used for all columns is returned by the TableHeader method getDefaultRenderer, which was added in 1.3. Sorting and Otherwise Manipulating Data One way to perform data manipulation such as sorting is to use one or more specialized table models (data manipulators), in addition to the table model that provides the data (the data model). The data manipulators should sit between the table and the data model, as the following picture shows:

[PENDING: This figure will be updated.] If you decide to implement a data manipulator, take a look at TableMap.java
Note: The TableSorter class was updated in February, 2004. We recommend the newer version, as it has more features and fixes several bugs. If you currently use the old version, you can update to the new one by deleting TableMap.java and making the following change in the code that sets up the sorter: //sorter.addMouseListenerToHeaderInTable(table); //OLD sorter.setTableHeader(table.getTableHeader()); //NEW
The setTableHeader method sets up the table's column headers to detect user clicks and update the UI appropriately. When the user clicks, the sorter sorts the rows based on the clicked column. As the following snapshot shows, when you click "Last Name", the rows are reordered so that the row with "Campione" becomes the first row.

[PENDING: This figure will be updated.] You can add secondary sorting by Control-clicking additional columns. For example, by clicking the Vegetarian column and then Control-clicking the Last Name column, you get an alphabetized listing of vegetarians followed by an alphabetized listing of omnivores. [PENDING: We will add a figure showing secondary sorting.] You can change the clicking behavior by reimplementing the MouseHandler class defined in TableSorter.java.
The Table API The tables in this section cover just part of the table API. For more information about the table API, see the API documentation for JTable
The component that presents the table to the user.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/JTableHeader.html]JTableHeader[/url]
The component that presents the column names to the user. By default, the table generates this component automatically.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableModel.html]TableModel[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/AbstractTableModel.html]AbstractTableModel[/url]
Respectively, the interface that a table model must implement and the usual superclass for table model implementations.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableCellRenderer.html]TableCellRenderer[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/DefaultTableCellRenderer.html]DefaultTableCellRenderer[/url]
Respectively, the interface that a table cell renderer must implement and the usual implementation used.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableCellEditor.html]TableCellEditor[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/DefaultCellEditor.html]DefaultCellEditor[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/AbstractCellEditor.html]AbstractCellEditor[/url]
Respectively, the interface that a table cell editor must implement, the usual implementation used, and the usual superclass for table cell editor implementations.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumnModel.html]TableColumnModel[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/DefaultTableColumnModel.html]DefaultTableColumnModel[/url]
Respectively, the interface that a table column model must implement and the usual implementation used. You don't usually need to deal with the table column model directly unless you need to get the column selection model, or get a column index or object.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html]TableColumn[/url]
Controls all the attributes of a table column, including resizability; minimum, preferred, current, and maximum widths; and an optional column-specific renderer/editor.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/DefaultTableModel.html]DefaultTableModel[/url]
A Vector-based table model used by JTable when you construct a table specifying no data model and no data.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/TableModelListener.html]TableModelListener[/url]
The interface that an object must implement to be notified of changes to the TableModel.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/event/ListSelectionListener.html]ListSelectionListener[/url]
The interface that an object must implement to be notified of changes to the table's selection.
Creating and Setting Up a Table Constructor or Method Purpose [url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#JTable(javax.swing.table.TableModel)]JTable(TableModel)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#JTable(javax.swing.table.TableModel,]JTable(TableModel, TableColumnModel)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#JTable(javax.swing.table.TableModel,]JTable(TableModel, TableColumnModel, ListSelectionModel)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#JTable()]JTable()[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#JTable(int,]JTable(int, int)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#JTable(java.lang.Object[][],]JTable(Object[][], Object[])[],]
Create a table. The optional TableModel argument specifies the model that provides the data to the table. The optional TableColumnModel and ListSelectionModel arguments let you specify the table column model and the row selection model. As an alternative to specifying a table model, you can supply data and column names, using arrays or vectors. Another option is to specify no data, optionally specifying the number of rows and columns (both integers) to be in the table.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setPreferredScrollableViewportSize(java.awt.Dimension)]void setPreferredScrollableViewportSize(Dimension)[/url]
Set the size of the visible part of the table when it's viewed within a scroll pane.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#getTableHeader()]JTableHeader getTableHeader()[/url]
Get the component that displays the column names.
Manipulating Columns Constructor or Method Purpose [url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#getColumnModel()]TableColumnModel getColumnModel()[/url]
(in JTable)
Get the table's column model.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumnModel.html#getColumn(int)]TableColumn getColumn(int)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumnModel.html#getColumns()]Enumeration getColumns()[/url]
(in TableColumnModel)
Get one or all of the TableColumn objects for the table.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#setMinWidth(int)]void setMinWidth(int)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#setPreferredWidth(int)]void setPreferredWidth(int)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#setMaxWidth(int)]void setMaxWidth(int)[/url]
(in TableColumn)
Set the minimum, preferred, or maximum width of the column.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#getMinWidth()]int getMinWidth()[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#getPreferredWidth()]int getPreferredWidth()[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#getMaxWidth()]int getMaxWidth()[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#getWidth()]int getWidth()[/url]
(in TableColumn)
Get the minimum, preferred, maximum, or current width of the column.
Using Editors and Renderers Method Purpose [url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setDefaultRenderer(java.lang.Class,]void setDefaultRenderer(Class, TableCellRenderer)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setDefaultEditor(java.lang.Class,]void setDefaultEditor(Class, TableCellEditor)[/url]
(in JTable)
Set the renderer or editor used, by default, for all cells in all columns that return objects of the specified type.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#setCellRenderer(javax.swing.table.TableCellRenderer)]void setCellRenderer(TableCellRenderer)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#setCellEditor(javax.swing.table.TableCellEditor)]void setCellEditor(TableCellEditor)[/url]
(in TableColumn)
Set the renderer or editor used for all cells in this column.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html#getHeaderRenderer()]TableCellRenderer getHeaderRenderer()[/url]
(in TableColumn)
Get the header renderer for this column. Version Note: As of 1.3, this method returns null if the column uses the default renderer. You generally use the TableHeader method getDefaultRenderer instead.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/JTableHeader.html#getDefaultRenderer()]TableCellRenderer getDefaultRenderer()[/url]
(in JTableHeader)
Get the header renderer used when none is defined by a table column. Introduced in 1.3.
Implementing Selection Method Purpose [url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setSelectionMode(int)]void setSelectionMode(int)[/url]
Set the selection intervals allowed in the table. Valid values are defined in [url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ListSelectionModel.html]ListSelectionModel
void setSelectionModel(ListSelectionModel)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#getSelectionModel()]ListSelectionModel getSelectionModel()[/url]
Set or get the model used to control row selections.
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setRowSelectionAllowed(boolean)]void setRowSelectionAllowed(boolean)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setColumnSelectionAllowed(boolean)]void setColumnSelectionAllowed(boolean)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setCellSelectionEnabled(boolean)]void setCellSelectionEnabled(boolean)[/url]
Set the table's selection orientation. The boolean argument specifies whether that particular type of selection is allowed. By default, row selection is allowed, and column and cell selection are not.
Examples that Use Tables This table lists examples that use JTable and where those examples are described. Example Where Described Notes [url=http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#SimpleTableDemo]SimpleTableDemo
A basic table with no custom model. Does not include code to specify column widths or detect user editing.
Adds single selection and selection detection to SimpleTableDemo. By modifying the program's ALLOW_COLUMN_SELECTION and ALLOW_ROW_SELECTION constants, you can experiment with alternatives to the table default of allowing only rows to be selected.
A basic table with a custom model.
Using an Editor to Validate User-Entered Text
Modifies TableDemo to use a custom editor (a formatted text field variant) for all Integer data.
Using a Combo Box as an Editor
Modifies TableDemo to use a custom editor (a combo box) for all data in the Sport column. Also intelligently picks column sizes. Uses renderers to display tool tips for the sport cells.
Modifies TableDemo to have a cell renderer and editor that display a color and let you choose a new one, using a color chooser dialog.
Specifying Tool Tips for Cells, Specifying Tool Tips for Column Headers,
Demonstrates how to use several techniques to set tool tip text for cells and column headers.
Sorting and Otherwise Manipulating Data
Sorts column data by interposing a data manipulating table model between the data model and the table. Detects user clicks on column headers.
How to Write a List Selection Listener
Shows how to use all list selection modes, using a list selection listener that's shared between a table and list.
Nowhere
Builds on ListSelectionDemo making the data model be shared between the table and list. If you edit an item in the first column of the table, the new value is reflected in the list.
TreeTable, TreeTable II
Creating TreeTables in Swing, Creating TreeTables: Part 2
Examples that combine a tree and table to show detailed information about a hierarchy such as a file system. The tree is a renderer for the table.

Start of Tutorial > Start of Trail > Start of Lesson
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.