java adding data to a JTable with a for-loop
i need some help writing a for-loop please but i can't get it.
i need to add the numbers 0 to 63 in a table like this: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 etc.
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
开发者_JAVA技巧 table.setValueAt(""+(i+(j)), i, j);
}
}
but the inputted values are not correct can you please help
(Assuming you're trying to do this so you count left to right top to bottom and have 8 columns)
You need to offset the value each time for the number of columns you've seen. Every time you iterate through an entire row, you've seen 8 columns, so it's not sufficient to add i
and j
, you need to add rows seen (i
) multiplied by the number of columns (8). Thus you should be printing i*8 + j
:
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
table.setValueAt(""+(i*8+j), i, j);
}
}
精彩评论