JACK JACK - 4 years ago 92
Java Question

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download