DefaultTableModel Overview

A programmer at work

 

Hero Images / Getty Images

The

DefaultTableModel

class is a subclass of the

AbstractTableModel

. As the name suggests it is the table model that is used by a

when no table model is specifically defined by the programmer. The DefaultTableModel stores the data for the JTable in a

Vector

of

Vectors

.

Although the

Vector

is a legacy Java collection it is still supported and there is no issue with using it unless the additional overhead caused by using a synchronized collection is a problem for your Java application.

The advantage of using the

DefaultTableModel

over a custom

AbstractTableModel

is you don't have to code the methods like add, insert or delete rows and columns. They already exist to change the data held in the

Vector

of

Vectors.

This makes it a quick and easy table model to implement.

Import Statement

import javax.swing.table.DefaultTableModel;

Constructors

The

DefaultTableModel

class has six

. Each can be used to populate of the

DefaultTableModel

in different ways.

The first constructor takes no arguments and creates a

DefaultTableModel

which has no data, zero columns and zero rows:

DefaultTableModel defTableModel = DefaultTableModel();

The next constructor can be used to specify the number of rows and columns of a

DefaultTableModel

with no data:

DefaultTableModel defTableModel = DefaultTableModel(10, 10);

There are two constructors that can be used to create a

DefaultTableModel

with column names and a specified number of rows (all containing null values). One uses an ​

Object

array to hold the column names, the other ​a

Vector

:

or

DefaultTableModel defTableModel = DefaultTableModel(columnNames, 10);

Finally there are two constructors used to populate the

DefaultTableModel

with row data along with column names. One used

Object

arrays, the other

Vectors

:

or

Useful Methods

To add a row to the

DefaultTableModel

use the

addRow

method along with the row data to add:

To insert a row use the

insertRow

method, specifying the row index to insert and the row data:

To delete a row use the

removeRow

method, specifying the row index to delete:

defTableModel.removeRow(0);

To get a value in a table cell use the

getValueAt

method. For example, if the data at row 2, column 2 contains an int:

int value = tabModel.getValueAt(2, 2);

To set a value in a table cell

setValueAt

method with the value to set along with the row and column index:

defTableModel.setValueAt(8888, 3, 2);

Usage Tips

If a

JTable

is created using the constructor that is passed a two-dimensional array containing the row data and an array containing the column names:

then the following cast will not work:

A runtime

ClassCastException

will be thrown because in this instance the

DefaultTableModel

is declared as an

in the

JTable

object and cannot be cast. It can only be cast to the

TableModel

interface. A way around this is to create your own

DefaultTableModel

and set it to be the model of the

JTable

:

Then the

DefaultTableModel
defTableModel

can be used to manipulate the data in the

JTable

.

To see the

DefaultTableModel

in action have a look at the

.