Java打印0来清空JTable中的单元格

时间:2017-02-03 08:56:39

标签: java printing jtable cell

在Java JTable中,我将此代码用于将print 0写入空单元格:

for (int i8 = 0; i8 < tgelir.getRowCount(); i8++) {
    if (dtmgelir.getValueAt(i8, 1) == null) {
        dtmgelir.setValueAt(0, i8, 1);
    } else {
        ...
    }
}

tgelirJTabledtmgelirDefaultTableModel

当我向单元格写入数字时,其他单元格变为0.但是当我在单元格中删除0并写入数字时,我之前写的数字变为0。

1 个答案:

答案 0 :(得分:0)

你需要使用两个循环来循环抛出所有单元格,如下所示:

//get rows and columns of your JTable
int nRow = yourtable.getRowCount(), nCol = yourtable.getColumnCount();

//Loop row by row
for (int i = 0; i < nRow; i++) {
    //each row loop its columns 
    for (int j = 0; j < nCol; j++) {
        //check if your value is not empty or null then change to 0
        if (yourtable.getValueAt(i, j).equals("") || yourtable.getValueAt(i, j) == null) {
            yourtable.setValueAt(0, i, j);
        }
    }
}

希望这可以帮到你。