Java – defaulttablemodel does not return the value in the datavector
My question is related to JTable and defaulttablemodel
DefaultTableModel model=(DefaultTableModel)jTable1.getModel(); int totalrows=model.getRowCount(); for (int i = totalrows - 1 ; i >= 0; i--) { Boolean checked = (Boolean) jTable1.getModel().getValueAt(i,8); if (checked) { ((DefaultTableModel)jTable1.getModel()).removeRow(i); } }
It always returns 0 from the getrowcount () method, so it won't go inside the loop The table already has rows
My tablemodel class is as follows
public class tabmod extends DefaultTableModel { public tabmod(Object rowData[][],Object columnNames[]) { super(rowData,columnNames); } @Override public Class getColumnClass(int col) { if (col == 8) return Boolean.class; return super.getColumnClass(col); //other columns accept String values } @Override public boolean isCellEditable(int row,int col) { return col == 8; } }
And I'm calling it from my JFrame.
tabmod tab=new tabmod(t,header); final JTable table = new JTable(tab); table.setFillsViewportHeight(true); jScrollPane1.add(table); jScrollPane1.setViewportView(table);
I didn't get the problem here
Solution
If you say that the row count is zero, but there are rows displayed on the visible GUI in the table, it means that your code does not reference the visible table, which means that you may have created an instance variable and a local variable for the table
final JTable table = new JTable(tab);
The above line may be wrong You should not use final variables Instead, you should use instance variables So the code should be:
table = new JTable(tab);
Now your deleterows () method can reference instance variables
//jScrollPane1.add(table); jScrollPane1.setViewportView(table);
You should never add components to a scrolling pane You only need to add the component that executes correctly in the second line Get rid of the front line