简体   繁体   English

增加JTable中的行数

[英]Increase row count in a JTable

I'm developing an address book for my classmates but I'm having a problem with a JTable . 我正在为同学们编写通讯录,但是JTable出现了问题。 Here you can see a preview of the program, I'm using NetBeans [ click ]. 在这里,您可以看到程序的预览,我正在使用NetBeans [ 单击 ]。 If you click Add to the Address Book , the program adds a new row in that table and fills its cells with the data located in text fields below. 如果单击Add to the Address Book ,程序将在该表中添加新行,并用下面文本字段中的数据填充其单元格。 I'm using the following code but the row count doesn't increase. 我正在使用以下代码,但行数不会增加。

界面原样

 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 advice as to how to fix this problem? 您能给我一些有关如何解决此问题的建议吗?

You created a new model. 您创建了一个新模型。 You should take the model that is assigned to the table. 您应该采用分配给表格的模型。

DefaultTableModel model = new DefaultTableModel();

should be: 应该:

DefaultTableModel model = jTable1.getModel();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM