Ketan Sachan
Ketan Sachan

Reputation: 95

JTable Automatic Empty Value Reading Issue

I am reading values of Second column from JTable . Everything works perfectly until it comes to last row. When I am reading the value of last row from the second column, it reads the empty string from the cell. I have invested my complete day to find the solution of this problem, but unfortunately result was unexpected.

Code is given below.

DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
int rows = Integer.parseInt(jTextField8.getText());
String strArr[] = new String[rows];
int count = 0;
System.out.println("Rows : "+rows);
for(int i=0; i<rows; i++){
    for(int j=0 ; j<3; j++){
        if(j==1){
            System.out.println(" i : "+i+" \tj : "+j);
            strArr[count] = (String)tableModel.getValueAt(i, j);
            System.out.println("Value \t: "+(String)tableModel.getValueAt(i, j));
            count++;
        }
    }
}

Table from where I am reading the values:

enter image description here

Output :

enter image description here

Upvotes: 1

Views: 48

Answers (1)

camickr
camickr

Reputation: 324098

Looks to me like your editor is still active so the value you typed into the editor has not yet been saved to the TableModel.

In the ActionListener of your button you need to add logic like:

if (table.isEditing())
    table.getCellEditor().stopCellEditing();

Or, when you create the table you can use:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

Check out: Table Stop Editing for more information.

Upvotes: 1

Related Questions