The JTable Class

Table Columns

With Swing tables, the basic unit is not an individual cell but a column. Most columns in real-world tables represent a certain type of information that is consistent for all records. For example, a record containing a person’s name is a String and might be the first column of the table. For every other record (row), the first cell is always a String. The columns do not need to all have the same data type. The same record could hold not only a person’s name, but whether or not they owned a computer. That column would hold Boolean values, not String values. The models supporting JTable reflect this view of the world. There is a TableModel that handles the contents of each cell in the table. You will also find a TableColumnModel that tracks the state of the columns in the table (how many columns, the total width, whether or not you can select columns, etc.).
The ability to store different types of data also affects how the table draws the data. The table column that maps to the “owns a computer” field could use a JCheckBox object for the cells of this column while using regular JLabel objects for the cells of other columns. But again, each column has one data type and one class responsible for drawing it.
Now, as the JTable class evolves, you may find alternate ways to think about tables without relying so heavily on columns. You’ll want to keep an eye on the API in future releases of the Swing package.
Well, we made it this far without officially discussing the JTable class itself. Dynamic data and database queries are handled entirely by the table model underneath the display. So what can you do with a JTable object? The JTable class gives you control over the appearance and behavior of the table. You can control the spacing of columns, their resizability, their colors, and so on. The JTable object is the source of row selections. Through delegation, you can add and remove rows and columns directly with the JTable object.

import java.awt.*;
import javax.swing.*;
import java.util.Date;

public class TableFeature extends JFrame {

	String titles[] = new String[] { "Directory?", 
			"File Name","Read?", 
			"Write?", "Size", "Last Modified" };

	public TableFeature() {
		super("Simple JTable Test");
		setSize(300, 200);

		File pwd = new File(".");
		Object[][] stats = getFileStats(pwd);

		JTable jt = new JTable(stats, titles);

		JScrollPane jsp = new JScrollPane(jt);
		getContentPane().add(jsp, BorderLayout.CENTER);

	public Object[][] getFileStats(File dir) {
		String files[] = dir.list();
		Object[][] results = new Object[files.length][titles.length];

		for (int i = 0; i < files.length; i++) {
			File tmp = new File(files[i]);
			results[i][0] = new Boolean(tmp.isDirectory());
			results[i][1] = tmp.getName();
			results[i][2] = new Boolean(tmp.canRead());
			results[i][3] = new Boolean(tmp.canWrite());
			results[i][4] = new Long(tmp.length());
			results[i][5] = new Date(tmp.lastModified());
		return results;

	public static void main(String args[]) {
		TableFeature tf = new TableFeature();