Reputation: 465
I have a frame with a table that get features from database (feature that correspond to the search). Here's the table :
public JTable getTableBornes() {
if(TableBornes==null){
TableModel TableBornesModel =
new DefaultTableModel(
new String[][] { { "", "", "" } },
new String[] { "Nom", "X", "Y" });
TableBornes = new JTable();
TableBornes.setModel(TableBornesModel);
TableBornes.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
TableBornes.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
}
return TableBornes;
}
What I want to do is, when the user click on a cell and edits it, this must be done on the database too. I tried to add a listener to the cellEditor
like this:
addBornes.getTableBornes().getCellEditor()
.addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
try {
for (int i = 0; i < rowsToActOn().length; i++) {
String name = addBornes.getTableBornes()
.getValueAt(rowsToActOn()[i], 0)
.toString();
Double x = new Double(addBornes
.getTableBornes()
.getValueAt(rowsToActOn()[i], 1)
.toString());
Double y = new Double(addBornes
.getTableBornes()
.getValueAt(rowsToActOn()[i], 2)
.toString());
String updateQuery = "UPDATE bornes_salemed_valid SET x='"
+ x
+ "', y='"
+ y
+ "', name='"
+ name
+ "'";
PostgisDataStoreDriver postgisDataStore = (PostgisDataStoreDriver) context
.getWorkbenchContext()
.getRegistry()
.getEntries(
DataStoreDriver.REGISTRY_CLASSIFICATION)
.get(0);
java.sql.Connection connx = postgisDataStore
.getConn();
Statement st;
st = connx.createStatement();
st.executeUpdate(updateQuery);
connx.setAutoCommit(false);
connx.commit();
try {
refreshDataStoreLayerPlugin
.execute(context);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void editingCanceled(ChangeEvent e) {
// TODO Auto-generated method stub
}
});
But am getting this exception:
the cell editor is null
I don't know how to declare it and where exactly? Please help.
java.lang.NullPointerException
at com.vividsolutions.jump.workbench.ui.plugin.specific.ModifierBornesPlugIn.execute(ModifierBornesPlugIn.java:305)
at com.vividsolutions.jump.workbench.plugin.AbstractPlugIn$1.actionPerformed(AbstractPlugIn.java:130)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
Upvotes: 0
Views: 319
Reputation: 109823
remove all code about DB Connection from this code
whats is CellEditorListener probably don't to use code without deepest knowledge about Java, Swing, JTable and XxxTableCelEditor
possible scenario
never to open execute close Connection on fly in this case (long and hard Object), Connection could be opened on background from Workers Thread (there isn't important EDT at all) together with JTable
add TableModelListener to JTable, returns proper coordinates to last edited cell,
JTable doesn't returns previous value before Editort comited an new value to the XxxTableModel
better could be to use AbstractTableModel, then you will fire another background task from setValueAt(), use SwingWorker (for enjoy) or Runnable#Thread (for production code), please you can to pretty ignore my last two commnets inside (....)
AbstractTableModel
with ResultSet.CONCUR_UPDATABLE
implemented as methods in the model directlyUpvotes: 2