shounak
shounak

Reputation: 51

How to get value of Last cell in JTable?

I have a query ... code is running fine, but am not able to get value of last cell of last row and last column. Below is the code... pls guide

with this code am adding rows dynamically to JTable : if(e.getSource()==addb) {

        model.addRow(new Object[3]);
        repaint();

    }

Below is the code for getting values from JTable row wise and later on instead of System.out.println() am going to send data to database...

if(e.getSource()==submit)
    {
        int j = table.getRowCount();
        for(int row=1;row<j;row++)
        {
            for(int column=0;column<3;column++)
            {
                System.out.println("row  "+row+"   Column is  "+column);                    
                System.out.println(model.getValueAt(row, column));
            }
        }

    }

Upvotes: 0

Views: 4431

Answers (3)

shounak
shounak

Reputation: 51

Thanks Azad,
Its working superb now...
I have a edited the code little bit to get data from all the rows...

 int i= table.getRowCount()-1;
        int j= table.getColumnCount();

        for (int d=1;d<i+1;d++){   // will provide row wise data
        for(int k = 0 ; k<j ; k++)  // will provide column wise data
        {
          System.out.println("row num   "+d+"   "+model.getValueAt(d,k));
        }   }

Upvotes: 0

Azad
Azad

Reputation: 5055

Something like this :

int i= table1.getRowCount()-1;
int j= table1.getColumnCount();
Object [] value = new Object[j];
for(int k = 0 ; k<j ; k++)
{
value[k] = model.getValueAt(i,k);
}   

Also see this little example

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TableTest extends JFrame implements ActionListener{

    JTable table ;
    JButton button;
    public TableTest(){
        String []colNames = {"Subject","lecturer"}; 
        String [][] rowDatas = { {"Java Programming","Jon"},
                                 {"C++ Programming","Nuhara"},
                                 {"Mathematicz","Mike"},
                                 {"Database","Saran"}
                                };
        table = new JTable(rowDatas,colNames);

        button = new JButton("Show Last Record");
        button.addActionListener(this);

        this.add(table);
        this.add(button);
        this.setVisible(true);
        this.setSize(300,200);
        this.setDefaultCloseOperation(3);
        this.setLayout(new FlowLayout());
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int i= table.getRowCount()-1;
        int j= table.getColumnCount();
        Object [] value = new Object[j];
        for(int k = 0 ; k<j ; k++)
        {
        //value[k] = table.getValueAt(i,k);
            System.out.println(table.getValueAt(i, k));
        }  
    }


    public static void main(String...ag){
        new TableTest();
    }
}

Upvotes: 3

camickr
camickr

Reputation: 324128

Making a wild guess that you are editing the last cell when you click on the "Submit" button.

If so then see: Table Stop Editing.

Upvotes: 2

Related Questions