Hi everybody,
I have a Jtable and i am going to add an empty row after pressing a button named insert.
Whenever i put these following code in my class ,it works properly.
I should add after these 2 lines i create a JScrollPane and add it to the getContentPane().

model = new DefaultTableModel(data,columnNames);   
    model.insertRow(table.getRowCount(),new Object[]  {"","","","",""});

THE PROBLEM:
Whenever i put these 2 lines in a seperate function like addRow() and call this function after pressing the insert button , i see no changes in Jtable,i mean a new empty row isn't added to the table.
I think i should add a new table to the JScrollPane but since i'm a new programmer in java ,i don't know how to do this.

Can anyone help me please?

Hello,

model = new DefaultTableModel(data,columnNames);   
    model.insertRow(table.getRowCount(),new Object[]  {"","","","",""});

When you use this code, It will create a new DefaultTableModel object and add blank row into it. But this model have NO association with table Nor Table is using this Model so row will not be added.

You should use :

model = JTableObj.getTableModel();   
    model.insertRow(table.getRowCount(),new Object[]  {"","","","",""});

getTableModel() method will return the Model object that is being used by table to display rows.

Regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.