java - 如何在java netbeans中捕获空jtable?

标签 java swing jtable

在空文本字段中!lblUser.getText().trim().equals("") 在空 jtable 中怎么样?因为我困惑如何捕获空的 jtable

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.....");
    }
}

在 jtable 中怎么样?

请帮助我......提前致谢......

最佳答案

您可以查看它是否有任何数据行:

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

如果行数为0,则肯定为空。请注意,这不会测试表是否有行,而是测试行内的单元格为空。为此,您需要获取 JTable 的 TableModel 并迭代行中的每个单元格,检查单元格中的数据,如下所示:

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;
}

关于java - 如何在java netbeans中捕获空jtable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47238695/

相关文章:

java - eclipse中我的项目顶部出现灰色标记的原因是什么?

java - JTable 不显示 SQL 准备语句的 AS 部分中设置的列名

java - 如何在 jpanel 之间切换?

java - 使用 AffineTransform 缩放图形

java - 从文件到 JTable

java - 更改调整 JTable 标题光标的大小

java - 如何保证只创建一个XMPPConnection?使用单例?

java - 在文件名中使用 .txt 时抛出异常

java - 如何找到给定数组的大小 >= 2 的最大子数组?

java - 如何用Java制作按钮绘画?