![]() ![]() |
![]() |
![]() ![]() ![]() ![]() |
![]() |
Swing implements tables using the JTable class, a subclass of JComponent. A JTable component is a user-interface component that presents data in a two-dimensional table format. With the help of JDBC, the database-interface mechanism that is designed to be used with JFC, tables implemented using JTable are automatically database-connectable; that is, they can access and manipulate data stored in relational databases.
To learn more about how JTable components can be used with relational databases, see Page 2 of this document set, "DB Connectivity."
For a hands-on demonstration of how JTable's database connectivity works, try out the TableExample program, a sample application that is provided with Swing. The TableExample application creates a table that you can to a remote database (such as Sybase or Microsoft SQL Server) or to a local database (such as MS Access). You can then view or manipulate the data stored in the RDBMS to which you have connected. The "DB Connectivity" page explains how to run and experiment with the TableExample program.
Table components instantiated from the JTable class have the following key features:
With the release of Version 0.5 of Swing (a preliminary pre-beta release), the JTable API was completely rewritten and extensively streamlined. Many new convenience methods were introduced, making the JTable class much easier to use. Also a new class -- named DefaultTableModel -- was introduced to simplify the use of the JTable class even more.
New methods that were added to JTable in Swing 0.5 included the following (see the JavaDoc APIs for DefaultTableModel and JTable for more details of these methods and many other new features of the JTable class):
public JTable(Vector data, Vector columnNames) public JTable(Object[][] data, Object[] columnNames) static public JScrollPane createScrollPaneForTable(JTable aTable) public void setAutoCreateColumnsFromModel(boolean createColumns) public void createDefaultColumnsFromModel() public Object getValueAt(Object columnIdentifier, int rowIndex) public void setValueAt(Object aValue, Object columnIdentifier, int rowIndex) public boolean isCellEditable(Object columnIdentifier, int rowIndex) public TableColumn addColumn(Object columnIdentifier, Vector columnData) public TableColumn addColumn(Object columnIdentifier, int width) public TableColumn addColumn(Object columnIdentifier, int width, TableCellRenderer renderer, TableCellEditor editor) public TableColumn addColumn(Object columnIdentifier, int width, TableCellRenderer renderer, TableCellEditor editor, Vector columnData) public void addRow(Vector rowData) public void addRow(Object[] rowData) public void insertRow(int rowIndex, Vector rowData) public void removeRow(int rowIndex) public void setNumRows(int newSize) public void setDataVector(Vector newData, Vector columnIds) public void setDataVector(Object[][] newData, Object[] columnIds)
Along with the methods listed in the preceding section, many other new features were added to the JTable class with the debut of Preliminary Version 0.5 of Swing. New features of JTable include support for the following (for more comprehensive details, see the JTable API):
Along with these new features, the TableModel interface was considerably expanded on Swing 0.5 to provide a richer set of information. For example, this added information makes it possible for developers to build model filter chains, which are very powerful tools. With model filter chains, a developer can set the data model of a JTable to a sorting filter that is a TableModel. In turn, a sorting filter created in this way has a TableModel that holds the real data that is to be displayed in a table. In this way, developers can build many different kinds of general filters and apply them in any order.
In another improvement that made its debut with the release of Swing
0.5, the AbstractTableModel was expanded to minimize code needed for the
simpler models. And finally, the JTable class was equipped with sorting
support.
Version 0.5. Last modified 10/1/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.