Source Code : How to add row in JTable?
How to add row in JTable?
TheTableModelbehind the JTable handles all of the data behind the table. In order to add and remove rows from a table, you need to use aDefaultTableModel
To create the table with this model:
<code>JTable table =newJTable(newDefaultTableModel(newObject[]{"Column1","Column2"}));</code>
To add a row:
<code>DefaultTableModel model =(DefaultTableModel) table.getModel();
model.addRow(newObject[]{"Column 1","Column 2","Column 3"});</code>
You can also remove rows with this method.
Full details on the DefaultTableModel can be foundhere.
Use:
<code>DefaultTableModel model =newDefaultTableModel();JTable table =newJTable(model);// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");// Append a row
model.addRow(newObject[]{"v1","v2"});</code>