This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Urgent! jtable get value when cell is edited Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Urgent! jtable get value when cell is edited" Watch "Urgent! jtable get value when cell is edited" New topic
Author

Urgent! jtable get value when cell is edited

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Hi,
I have a JTable with model extended from DefaultTableModel. The table is in a frame with a Save button. If a cell is edited & when I click on Save , the new cell values should be obtained.Im not able to get the updated values on Save. Im storing the selected row in a vector. Any suggestions pls.
Vinod


- Vinod<br />-------<br />SCJP2
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
I think there are 2 ways to do that.
One way is to addTableModelListener to your tablemodel.
yourTableModel.addTableModelListener(new TableModelListener() {
public void tableChanged (TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
...// Do something with the data...
}
}
});
Another way is to override setValueAt method in your tableModel.
public void setValueAt(Object value, int row, int col) {
//You get rowNumber , columnNumber and new value here
//Do something with the data.
}
At last there is something that you have to know. If the CellEditor doesn't get a "stopCellEditing" call, the underlying DataModel doesn't get updated via setValueAt(value, row, col).So you have to stop the CellEditor programmatically by adding one line yourTable.getCellEditor().stopCellEditing();
Good luck!

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Thanx for the suggestion..but I need to press Enter to register the changes & then press Save button to send the changed data. Any way to bypass pressing Enter?( using ur tablemodellistener)
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
tablename.getModel().getValueAt(row, col)
This method will return an Object. Just cast it to whatever it should be for the given col.
george justin
Greenhorn

Joined: Jul 31, 2001
Posts: 25
Originally posted by Vinod Venugopal:
Hi,
I have a JTable with model extended from DefaultTableModel. The table is in a frame with a Save button. If a cell is edited & when I click on Save , the new cell values should be obtained.Im not able to get the updated values on Save. Im storing the selected row in a vector. Any suggestions pls.
Vinod

Hi Vinod
in the action performed method of the button, u can write
.
.
table.getCellEditor().stopCellEditing();
.
.
this works well..
i hope u have Overridden
setValueAt(..)

method from AbstractTableModel or DefaultTableModel class.
getCellEditor returns and instance of DefaultCellEditor if u have not subclassed any CellEditor, if so cast it to the corresponding CellEditor..
Try out if not mail me i will mail u the code.
Good Luck
George Justin




Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
I tried overriding setValueAt() & calling fireTablecellupdated() but still it doesnt register the new cell value when I click on my Save button. Could u pls send me the piece of code that could help.
Thanx,
Vinod
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vinod,
Using Georges' suggestion, I did the following and it worked great.

Just type in some value and hit the dump button to see the typed results (without hitting return!).
Regards,
Manfred.
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Thanx a lot Manfred & everybody else who gave me suggestions, it worked out fine.
Regards,
Vinod
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent! jtable get value when cell is edited
 
Similar Threads
JTable
problem on JTable
Jtable Cell editing validations
JTable
JTable Editing