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.
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 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 .
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With