Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How loop through all rows of a jtable

I'm trying to loop through all of the rows in a column in a jTable at the moment I can get it to loop through a column but it only gives me the first 5 values and it also gives me a strange output.

here is the code:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // Button to Start
    Object[] columnData = new Object[jTable1.getColumnCount()];
    Object[] rowData = new Object [jTable1.getRowCount()];
    for (int i = 0; i < jTable1.getColumnCount(); i++) {
    columnData[i] = jTable1.getValueAt(i, 4);
    System.out.println(Arrays.toString(columnData));
    }

here is the output:

enter image description here

like image 479
Kevin Medjiako Avatar asked Oct 28 '25 17:10

Kevin Medjiako


1 Answers

I think you are using your column iteration as the row number in your code. jTable1.getValue(i, 4) has parameters row, column in that order. If you only have five columns, you will only get five values.

Try changing the loop to count through the rows and select the 5th column.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // Button to Start
    Object[] columnData = new Object[jTable1.getRowCount()];  // One entry for each row
    Object[] rowData = new Object [jTable1.getRowCount()];
    for (int i = 0; i < jTable1.getRowCount(); i++) {  // Loop through the rows
        // Record the 5th column value (index 4)
        columnData[i] = jTable1.getValueAt(i, 4);  
     }
     System.out.println(Arrays.toString(columnData));
like image 113
Michael McKay Avatar answered Oct 31 '25 06:10

Michael McKay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!