0

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));
            }
        }

    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
shounak
  • 51
  • 2
  • 8
  • 3
    1. everything depends of your `XxxTableModel`, for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, just about `JTable` and your `XxxTableModel`, 2. without any query (hardcode that as local variable == some type of array), 3. the same or similair question is asked 2-3times per day – mKorbel Feb 23 '13 at 15:34
  • You first need to define what the last cell is. Is it the last one in the view (which might be sorted and filtered, with columns reordered), or is it the last one in the model? – JB Nizet Feb 23 '13 at 15:58
  • @JB Nizet : The cell is last cell in the last row. Am adding rows dynamically in the model. and Whatever number of rows i may add.. am facing this problem only at last cell of last row. – shounak Feb 24 '13 at 03:01

3 Answers3

3

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();
    }
}
Azad
  • 5,047
  • 20
  • 38
  • It's not working. Am still getting value of last cell an 'null' ! Thanks for giving try. – shounak Feb 24 '13 at 03:09
  • you can try value[k] = table.getValueAt(i,k); – Azad Feb 24 '13 at 08:27
  • thanks for your efforts, but its still not working. Am tired of this now... getting frustrated ! Am not getting what is going wrong with the code..... if this thing doesn't work by 2morrow the i'll use some different way.. i will keep whole last row editable(false) and will not consider that row at all... i know thats not correct way to go through it... but i am not seeing any hopes now..! once again thanks for ur efforts ! – shounak Feb 25 '13 at 03:27
  • 1
    you'r welcome , See I edited my answer. by the way, you probably have an error in your table unless I used the same function to get the last value and it's working. see the example hope it's will be useful . – Azad Feb 25 '13 at 11:13
2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • actually am adding empty rows through addb actiolistener, and then am editing cells with reuired values and then by using submit actionlistener am sending data from all the edited cells to database... but unfortunately every time ma getting value of last cell as null. am not getting why is that happening. – shounak Feb 24 '13 at 02:50
  • Did you read the link I gave you? If that isn't the problem then you need to post an SSCCE. – camickr Feb 24 '13 at 05:57
0

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));
        }   }
shounak
  • 51
  • 2
  • 8