Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages


How to Use Tables

With the JTable(in the API reference documentation) class, you can display tables of data. JTable doesn't contain or cache data; it's simply a view of your data. This lack of caching makes JTable perform well, even when its data comes from a huge database. Because JTable has too many features to be completely described in this tutorial, please periodically search The Swing Connection for pointers to other table documentation.

When you create a table, you need to supply the data for that table. Generally, this means writing a subclass of the AbstractTableModel(in the API reference documentation) class. A table model contains methods for getting and setting data values, getting the number of rows (records) of data, and adding and removing table listeners. (Table listeners are objects such as the JTable that are notified each time the data changes.)

Here is a picture of an application that displays an uneditable table in a scroll pane:


Try this:
  1. Compile and run the application. The source file is SimpleTableDemo.java.
    See Getting Started with Swing if you need help.
  2. Click a cell.
    The entire row is selected. This is intended to remind you that JTable implements a database browser, not a spreadsheet.
  3. Position the cursor over the "First Name" heading. Now press the mouse button and drag the heading to the right.
    As you can see, users can rearrange columns in tables.
  4. Position the cursor just to the right of a heading. Now press the mouse button and drag to the right or left.
    The column changes size.
  5. Resize the window containing the table so that it's bigger than necessary to display the whole table.
    The table cells stay the same size, and they're clustered at the upper left of the display area.

Below is the code from SimpleTableDemo.java that implements the table in the previous example.

public class SimpleTableDemo extends JPanel {
    public SimpleTableDemo() {
        JTable table = new JTable(new MyTableModel());

        //Create the scroll pane and add the table to it. 
        JScrollPane scrollPane = JTable.createScrollPaneForTable(table);
        scrollPane.setPreferredSize(new Dimension(400, 100));

        //Add the scroll pane to this panel.
        setLayout(new GridLayout(1, 0)); 
        add(scrollPane);
    }

    class MyTableModel extends AbstractTableModel {
        final String[] columnNames = {"First Name", 
                                      "Last Name",
                                      "Sport",
                                      "Est. Years Experience"};
        final String[][] data = {
            {"Mary", "Campione", "Snowboarding", "5"},
            {"Alison", "Huml", "Rowing", "3"},
            {"Kathy", "Walrath", "Chasing toddlers", "2"},
            {"Mark", "Andrews", "Speed reading", "20"},
            {"Angela", "Lih", "Teaching high school", "4"}
        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
    }
    . . .
}
[PENDING: Now show an editable table.]


Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages