Of course, if you allow cell editing, you presumably want a listener to take note of that editing. The following illustrates the view side of such a listener. Nothing happens on the model side; the listener only prints edited value. The listener is a stub, just as the listener defined in Segment 1007 is a stub.
The listener implements the TableModelListener
interface, which imposes the
definition of tableChanged
, which is called by the machinery that
watches for editing events. The table-model event supplies information
about the change, identified by the getFirstRow
and
getColumn
methods. There is also a getLastRow
method, but
when you edit individual cells, the first row and the last row are the
same.
import javax.swing.event.*; import javax.swing.table.*; public class MovieTableCellListener implements TableModelListener { MovieTableApplication application; public MovieTableCellListener (MovieTableApplication a) { application = a; } public void tableChanged (TableModelEvent e) { int row = e.getFirstRow(); int column = e.getColumn(); if (row < 0 || column < 0) {return;} TableModel model = application.getRatingTable().getModel(); Object o = model.getValueAt(row, column); System.out.println("Edited value is " + o + " of " + o.getClass()); } }