简体   繁体   中英

Java printing 0 to empty cells in JTable

In Java JTable I am writing this code for print 0 to empty cell :

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

tgelir is a JTable , dtmgelir is a DefaultTableModel .

When I write a number to cell, other cells turn to 0. But when I delete 0 in a cell and write a number, the number I wrote earlier turns into 0.

You need to use two loops, to loop throw all your cells, row by row like this :

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

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM