0

I tried to create a table and manipulate it. but I do not understand what code should I write in my netbeans.

JTextField1 variable n JButton1 Variable ok JTable1 Variable tabel1

on JButton1 or Ok Button I give code like this:

private void okActionPerformed(java.awt.event.ActionEvent evt) {
    int key = Integer.parseInt(n.getText());
    c.setText(String.valueOf(key));
    DefaultTableModel dtm = (DefaultTableModel) tabel1.getModel();
    for(int i = 0; i < key; i++){
    int r = tabel1.getRowCount()+1;
    dtm.setRowCount(r);
    }

}  

with code that I write, I managed to make the line corresponds to the number on JTextField1.

but when i press the ok button continuously. rows in the table continues to grow.

how can i make that row in the table according to Jtextfield1 and no longer grow?

thanks before.

4

2 回答 2

1
    DefaultTableModel dtm = (DefaultTableModel) tabel1.getModel();
    dtm.setRowCount(rowcount); // instead you can use your textfiled value here
e.g. dtm.setRowCount(Integer.parseInt((txtFieldName.getText()));
    tabel1.setModel(dtm);
于 2013-10-28T06:25:00.043 回答
0
private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    int key = Integer.parseInt(n.getText());
    DefaultTableModel dtm = (DefaultTableModel) tabel1.getModel();
    dtm.setRowCount(0);
    for(int i = 0; i < key; i++){
    int r = tabel1.getRowCount()+1;
    dtm.setRowCount(r);
    }        
}      
于 2013-10-28T16:42:04.920 回答