In Netbeans, I used the GUI Builder to insert a JTable into my application.
I have just one class (CustomerDB) so far which is:
package CustomerDB;
import [...];
public class CustomerDB extends javax.swing.JFrame {
CellEditorListener ChangeNotification = new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
System.out.println("The user canceled editing.");
}
public void editingStopped(ChangeEvent e) {
System.out.println("The user stopped editing successfully.");
}
};
public CustomerDB() {
customerTable = new javax.swing.JTable();
customerTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"ID", "Name", "Address", "Phone"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
customerTable.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CustomerDB().setVisible(true);
}
});
}
// Variables declaration - do not modify
[...]
private javax.swing.JTable customerTable;
[...]
// End of variables declaration
}
Whenever a user changes data in the table, I want to get the old value (optional) and the new value of that cell.
In order to get this data, I tried to implement an event listener:
CellEditorListener ChangeNotification = new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
System.out.println("The user canceled editing.");
}
public void editingStopped(ChangeEvent e) {
System.out.println("The user stopped editing successfully.");
}
};
Then I assign this CellEditorListener to the table (its cell editor):
customerTable.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification);
This works so far. But it doesn't yet enable me to detect the old and the new value of this cell. What else do I have to do?
Thank you very much in advance!
See Question&Answers more detail:
os