Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add row in JTable?

Tags:

java

swing

jtable

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

like image 741
oneat Avatar asked Aug 23 '10 15:08

oneat


People also ask

How do you add a new row in Java?

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

How add column in JTable in NetBeans?

Right-click the table and select "Properties." The table's properties display, including the columns. You click a column and change the heading to edit the current columns. To add a new column, click "New" and type a heading for the column.

How add data from Jtextfield to JTable?

To add the data entered in the JTextFields you will need to register an ActionListener to your add button, in this case jButton1 . To add entries to your table model you could use a mutable model such as DefaultTableModel : DefaultTableModel model = new DefaultTableModel(data, columns);


1 Answers

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

like image 122
Serplat Avatar answered Sep 25 '22 02:09

Serplat