How to add row in JTable?

oneat picture oneat · Aug 23, 2010 · Viewed 333.9k times · Source

Do you know how I can add a new row to a jTable?

Answer

Serplat picture Serplat · Aug 23, 2010

The TableModel behind the JTable handles all of the data behind the table. In order to add and remove rows from a table, you need to use a DefaultTableModel

To create the table with this model:

JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}));

To add a row:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});

You can also remove rows with this method.

Full details on the DefaultTableModel can be found here