Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a JTable is empty?

Tags:

java

swing

jtable

Unlike most data structures, JTable does not have isEmpty() method. So how can we know if a given JTable doesn't contain any value?

like image 725
user3162879 Avatar asked Jun 02 '15 18:06

user3162879


People also ask

How to validate if JTable cell is empty in Java?

A JTable will generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces. We can validate whether the JTable cell is empty or not by implementing the getValueAt () method of JTable class.

What if we Cant check JTable?

So,my opinion is that if we cant check the jTable,check the Dataset which be written in the table. Share Improve this answer Follow edited Dec 25, 2017 at 5:49 answered Dec 22, 2017 at 11:25 BabafingoBabafingo 122 bronze badges 3 (1-) this code shows how to add data from a ResultSet to a table model.

What is a JTable in Java?

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable will generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces.

What is setvalueat () method in JTable?

setValueAt (Object value, int row, int col) : Sets the cell value as ‘value’ for the position row, col in the JTable. Below is the program to illustrate the various methods of JTable:


2 Answers

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

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

jTable.getRowCount() will return only visible row count. So to check isEmpty() then it's better to use getRowCount() on model.

public static boolean isEmpty(JTable jTable) {
        if (jTable != null && jTable.getModel() != null) {
            return jTable.getModel().getRowCount()<=0?true:false;
        }
        return false;
    }
like image 116
K139 Avatar answered Sep 22 '22 03:09

K139


table.getRowCount();
table.getColumnCount();

If either one is 0, then there is no data.

like image 22
camickr Avatar answered Sep 22 '22 03:09

camickr