如何获取JTable中非空的行数?

时间:2011-12-02 11:19:40

标签: java swing jtable

getRowCount()获取设置的行数,因此我无法使用它。

这就是我所做的,它不起作用:

private String[][] getContentsFromTable(){
    int cols= jTable1.getColumnCount();
    int rows=jTable1.getRowCount();
    System.out.print(rows);
    String items[][]= new String[rows][cols];
    if(jTable1.getCellEditor()!=null)
        jTable1.getCellEditor().stopCellEditing();        
    for(int i=0;i< jTable1.getRowCount();i++){
        for (int j=0;j<jTable1.getColumnCount();j++){
            //skip subtotal
            if(j!=3){
                if(jTable1.getValueAt(i,j)!=null){
                    String om=jTable1.getValueAt(i,j).toString();

                    if(om.trim().length()!=0){
                        items[i][j]=om;
                        //System.out.print(""+i+",j "+j+": "+items[i][j]);
                }
                }
            }
        }

    }
    return items;

}
编辑:该表是可编辑的,我需要从行中获取所有数据,所以我必须检查它并确定非空行的数量,以便我可以在循环中使用它们并将数据存储在数据库中< / p>

1 个答案:

答案 0 :(得分:0)

有点太晚,但对于遇到NULL行问题的人来说,.getValueAt会抛出一个空异常,所以不要试图用它来检查空行。