Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get changed values from JTable

 
gina meyer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
table.getSelectionModel().addListSelectionListener(lsm);
***************************************************
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();
try
{
System.out.println("Cell "+columnId+" :
"+table.getValueAt(rows[0],column));
StudentRow.add((String)table.getValueAt(rows[0],0));
StudentRow.add((String)table.getValueAt(rows[0],1));
StudentRow.add((String)table.getValueAt(rows[0],2));
StudentRow.add((String)table.getValueAt(rows[0],3));
StudentRow.add((String)table.getValueAt(rows[0],4));
StudentRow.add((String)table.getValueAt(rows[0],5));
setStudentRow(StudentRow);
}catch(Exception exp)
{}
....
**********************************
but I always get the old, unchanged values!!! even though I can see the changed values in the table!!!

cheers
Gina
gina.meyer@lineone.net
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a couple of other posts about updating tables:
http://www.javaranch.com/ubb/Forum2/HTML/000306.html
http://www.javaranch.com/ubb/Forum2/HTML/000218.html
http://www.javaranch.com/ubb/Forum23/HTML/000394.html
 
gina meyer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
reallocateIndexes();
super.tableChanged(e);
}
how can I get the complete row read into a Vector at that point???
Finding this would solve my problem!!!
Gina
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic