Increase the number of rows in JTable

I am developing an address book for my classmates, but I have a problem with JTable . Here you can see a preview of the program, I am using NetBeans [ click ]. If you click Add to the Address Book , the program will add a new row to this table and fill its cells with data located in the text fields below. I use the following code, but the number of lines does not increase.

GUI as is

  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { int h; DefaultTableModel model = new DefaultTableModel(); h=jTable1.getRowCount()+1; model.setRowCount(h); jTable1.setValueAt(jTextField2.getText(), h, 1); jTable1.setValueAt(jTextField3.getText(), h, 2); //I'll use more setValueAt() because I must fill all the cells } 

Could you give me some tips on how to fix this problem?

+4
source share
1 answer

You have created a new model. You must take the model assigned to the table.

 DefaultTableModel model = new DefaultTableModel(); 

it should be:

 DefaultTableModel model = jTable1.getModel(); 
+4
source

Source: https://habr.com/ru/post/1486804/


All Articles