Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase row count in a JTable

Tags:

java

swing

jtable

I'm developing an address book for my classmates but I'm having a problem with a JTable. Here you can see a preview of the program, I'm using NetBeans [click]. 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. I'm using the following code but the row count doesn't 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 advice as to how to fix this problem?

like image 405
Alberto Miola Avatar asked Jun 18 '13 11:06

Alberto Miola


People also ask

How can count number of rows in JTable?

You can use the getRowCount() method: Returns the number of rows that can be shown in the JTable , given unlimited space. If a RowSorter with a filter has been specified, the number of rows returned may differ from that of the underlying TableModel .

How do you size a JTable?

This can be done easily using these two methods of the JTable class: setRowHeight(int row, int rowHeight): sets the height (in pixels) for an individual row. setRowHeight(int rowHeight): sets the height (in pixels) for all rows in the table and discards heights of all rows were set individually before.

How do I get the value of a JTable cell?

Obtaining Cell Values To get the value from a particular grid cell, you can use the wValue property of the JTable object. The property has the Row and Column parameters which specify the row and the column that contain the cell.


1 Answers

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();
like image 181
darijan Avatar answered Nov 11 '22 11:11

darijan