Adding an Array to JTable in Java

19,208

Solution 1

You cannot add to a JTable directly, you have to get the underlying TableModel. You get this by calling JTable.getModel(). TableModel is an interface, in a standard JTable it's implementation is DefaultTableModel. So you have to cast the underlying TableModel to a DefaultTableModel, and then you can apply DefaultTableModel.addRow( Object[] ). (You do, of course, check that the cast is safe and all that).

Solution 2

To change the data displayed by the JTable, you need to go through the TableModel.

Have a look at the JTable.getModel() method and the methods in the TableModel interface.

Solution 3

there aren't some problem with that, here is How to Use Tables with nice example, tons of examples here and here

Solution 4

you can add/insert row in JTable like this way

table.getModel().insertRow(table.getRowCount(),new Object[]{"hello","50"});

here is the tutorial link

http://www.roseindia.net/java/example/java/swing/InsertRows.shtml

Share:
19,208
Jonas
Author by

Jonas

Updated on June 04, 2022

Comments

  • Jonas
    Jonas almost 2 years

    Since you create the JTable with an matrix for data and array for the columns I figured there should be a way to after created the JTable adding an array (row). Or how is it meant to add a row with Strings?

    Thanks!