Kimz Rayzor
Kimz Rayzor

Reputation: 3

How to trap in empty jtable in java netbeans?

In empty textfield !lblUser.getText().trim().equals("") how about in empty jtable? because i confuse how to trap the empty jtable

something like this same in the jtextfield...

public void InputUserPass() {
    if (!lblUser.getText().trim().equals("") & !txtPass.getPassword().equals("")) {
        Login();
    } else {
        JOptionPane.showMessageDialog(null, "Please fill-up the requirements information before saving.....");
    }
}

how about in jtable?

please help me..... thanks in advance...

Upvotes: 0

Views: 115

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You could see if it has any rows of data:

if (jTable.getRowCount == 0) {
    // the JTable jTable is empty
}

if the row count is 0, then it is definitely empty. Note that this does not test if the table has rows but the cells within the rows are empty. For that you'd need to get the JTable's TableModel and iterate through each cell in the rows checking the cells for data, something like:

public boolean isTableEmpty(JTable jTable) {
    TableModel tableModel = jTable.getModel();

    // if model has no rows -- table is empty
    if (tableModel.getRowCount == 0) {
        return true;
    }

    // if model has rows, check each cell for non-null data
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        for(int j = 0; j < tableModel.getColumnCount(); j++) {
            if (tableModel.getValueAt(i, j) != null) {
                // if any cell has data, then the table is not empty
                return false;
            }
        }
    }

    // all cells hold null values
    return true;
}

Upvotes: 1

Related Questions