How to find out if JTable is empty?

Unlike most data structures, JTable does not have an isEmpty() method. So, how can we find out if a given JTable contains any value?

+5
source share
4 answers
 table.getRowCount(); table.getColumnCount(); 

If one of them is 0, then there is no data.

+5
source

This will return the actual number of rows, regardless of any filters on the table.

 int count= jTable.getModel().getRowCount(); 

jTable.getRowCount() will return only the visible number of rows. Therefore, to check isEmpty (), it is better to use getRowCount() for the model.

 public static boolean isEmpty(JTable jTable) { if (jTable != null && jTable.getModel() != null) { return jTable.getModel().getRowCount()<=0?true:false; } return false; } 
+4
source

you have a fixed number of columns .. suppose three

 public int Actualrows(DefaultTableModel m) { int row = 0; int i = 0; for (i = 0; i < m.getRowCount(); ++i) { try { if (!(m.getValueAt(i, 0).equals("")) && !(m.getValueAt(i, 1).equals("")) && !(m.getValueAt(i, 2).equals(""))) { row++; } } catch (NullPointerException e) { continue; } } return row; } 

if the return value is 0 its completely empty table

0
source

There really is no way to recognize him, my friend. But I myself, using the method to find out if it is empty or not.

First, by creating a JTable , set the default value to 0 . If you want to use JTable , do a JTable with data that returns a score from the database. Therefore, if not, data.getRowCount() will return 0 again. So, you will find out if the JTable empty or not.

  try (ResultSet rs = st.executeQuery(query)) { int colcount = rs.getMetaData().getColumnCount();//get the row count in the database DefaultTableModel tm = new DefaultTableModel(); for (int i = 1; i <= colcount; i++) { tm.addColumn(rs.getMetaData().getColumnName(i)); } while (rs.next()) { Object[] row = new Object[colcount]; for (int i = 1; i <= colcount; i++) { row[i - 1] = rs.getObject(i); } tm.addRow(row); } jTblAd.setModel(tm); conn.close(); } 

Finally, I want to say that if jTable has a head, for example: Name-Surname-Age, etc., table.getRowCount(); table.getColumnCount(); table.getRowCount(); table.getColumnCount(); not working due to jtable chapter. So, I believe that if we cannot check jTable, check the dataset that will be written to the table.

-2
source

All Articles