• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Urgent! jtable get value when cell is edited

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tablename.getModel().getValueAt(row, col)
This method will return an Object. Just cast it to whatever it should be for the given col.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot Manfred & everybody else who gave me suggestions, it worked out fine.
Regards,
Vinod
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic