验证 jtable 是否为空?
Validate if jtable is empty?
我需要知道如何检查 JTable
中是否有数据,例如
if(jTextField1.getText().isEmpty()):
then (if jTable1.isEmpty())
...
我该怎么做?
JTables 不是像 jtextfield 这样的简单组件,像其他 swing 组件一样,它们有一个底层数据模型,请查看 javadoc 中的这个示例:
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
JTable table = new JTable(dataModel);
就像遵循 MVC 模式的每个 UI 对象一样,您不使用图形组件来理解它具有的值,而是使用数据模型。在您的情况下,保存对您创建的 JTable 的数据模型的引用并调用 getRowCount
以了解您之前加载了多少数据。
此外,查看官方文档here。
我验证 jtable 是否为空
此代码。
private int calculate() {
Vector<Integer> myvector = new Vector();
TableModel mode = new DefaultTableModel();
mode = jTable2.getModel();
int n = mode.getRowCount();
for (int i = 0; i < n; i++) {
if (mode.getValueAt(i, 3) != null) {
myvector.add((Integer) mode.getValueAt(i, 3));
}
}
return myvector.size();
}
//then I validate with a button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int numofvalidrows;
numofvalidrows = calculate();
if (numofvalidrows == 0) //if the size of the vector is 0 then the jtable is empty
{
System.out.println("You need to add people to the jtable, because the table is empty");
} else {
// I get the values of the jtable with
for (int i = 0; i < n; i++) {
if (model.getValueAt(i, 3) != null) { //whith this "if" I print only data, not print null of the empty cells in jtable
System.out.print(model.getValueAt(i, 3))
}
}
}
}
我需要知道如何检查 JTable
中是否有数据,例如
if(jTextField1.getText().isEmpty()):
then (if jTable1.isEmpty())
...
我该怎么做?
JTables 不是像 jtextfield 这样的简单组件,像其他 swing 组件一样,它们有一个底层数据模型,请查看 javadoc 中的这个示例:
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
JTable table = new JTable(dataModel);
就像遵循 MVC 模式的每个 UI 对象一样,您不使用图形组件来理解它具有的值,而是使用数据模型。在您的情况下,保存对您创建的 JTable 的数据模型的引用并调用 getRowCount
以了解您之前加载了多少数据。
此外,查看官方文档here。
我验证 jtable 是否为空 此代码。
private int calculate() {
Vector<Integer> myvector = new Vector();
TableModel mode = new DefaultTableModel();
mode = jTable2.getModel();
int n = mode.getRowCount();
for (int i = 0; i < n; i++) {
if (mode.getValueAt(i, 3) != null) {
myvector.add((Integer) mode.getValueAt(i, 3));
}
}
return myvector.size();
}
//then I validate with a button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int numofvalidrows;
numofvalidrows = calculate();
if (numofvalidrows == 0) //if the size of the vector is 0 then the jtable is empty
{
System.out.println("You need to add people to the jtable, because the table is empty");
} else {
// I get the values of the jtable with
for (int i = 0; i < n; i++) {
if (model.getValueAt(i, 3) != null) { //whith this "if" I print only data, not print null of the empty cells in jtable
System.out.print(model.getValueAt(i, 3))
}
}
}
}