如何在 java netbeans 中捕获空 jtable?
How to trap in empty jtable in java netbeans?
在空文本字段中 !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,则肯定是空的。请注意,这不会测试 table 是否有行,但行中的单元格为空。为此,您需要获取 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;
}
在空文本字段中 !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,则肯定是空的。请注意,这不会测试 table 是否有行,但行中的单元格为空。为此,您需要获取 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;
}