File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes get changed values from JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "get changed values from JTable" Watch "get changed values from JTable" New topic

get changed values from JTable

gina meyer

Joined: Feb 06, 2001
Posts: 5
Hi GUrus,
I am not an experienced programmer, as you can easily see and would be very appreciative for any example code so that I can carry on doing my project.
It is extremely important as it is my final year degree project!!!
I posted it here cause I couldn't find this topic in any of my beginner/intermediate books.
What do I have to do to get the changed values when I amend data in my JTable???
I think it must be some TableModelListener but do not have
a clue how to write them. I would need the changed values
to do an update to the DB

I use a DefaultTableModel:
DefaultTableModel model = new DefaultTableModel(rows, header);
table = new JTable( model );
//** add a listener to get the user's selection
lsm = new MyTableSelectionListener();
in the listener code: when a selection is made I wanted to get the
selection's values read into a Vector
int column = table.getSelectedColumn();
int[] rows = table.getSelectedRows();
TableModel tm = table.getModel();
String columnId = tm.getColumnName(column);
Vector StudentRow = new Vector();
System.out.println("Cell "+columnId+" :
}catch(Exception exp)
but I always get the old, unchanged values!!! even though I can see the changed values in the table!!!

Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Here are a couple of other posts about updating tables:

"JavaRanch, where the deer and the Certified play" - David O'Meara
gina meyer

Joined: Feb 06, 2001
Posts: 5
Thanx, Cindy, for your replies!!! I am glad it helped me and I am a tiny bit away from it, I think!!!
I have amended my code now BUT the nice and neat things in these examples is that they are exactly what I do not have : which is a neat String[][] from which my JTable is built.
my table consists of DefaultDataModel(Vector rows, Vector header)
where rows is a Vector of Vectors (single row in returning resultset from DB)
HOW can I get this model be adapted to my Vectors??
// Create a model of the data.
TableModel model = new AbstractTableModel() {

// These methods always need to be implemented.
public int getColumnCount() { return header.size(); }
public int getRowCount() { return rows.size();}
public Object getValueAt(int row, int col) {
return rows[row][col];

public String getColumnName(int column) {
return (String)header.elementAt(column);

public Class getColumnClass(int col) {
return getValueAt(0,col).getClass();

public boolean isCellEditable(int row, int col) {
return (col==1);
public void setValueAt(Object aValue, int row, int column) {
rows[row][column] = aValue;
fireTableCellUpdated(row, col);


DefaultTableModel model = new DefaultTableModel(rows, header);
TableSorter sorter = new TableSorter(model);

table = new JTable(sorter);
the TableSorter and TableMap code is in demo/jfc/TableExample/src
what I get each time I amend the cell value
Sorter: tableChanged!! from the Sorter
public void tableChanged(TableModelEvent e)
System.out.println("Sorter: tableChanged");
how can I get the complete row read into a Vector at that point???
Finding this would solve my problem!!!
I agree. Here's the link:
subject: get changed values from JTable
It's not a secret anymore!