我有一个带有表格的框架,该表格从数据库中获取功能(与搜索相对应的功能)。这是表格:
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;
}
我想要做的是,当用户单击一个单元格并对其进行编辑时,这也必须在数据库上完成。我试图cellEditor
像这样添加一个监听器:
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
}
});
但是我得到了这个例外:
单元格编辑器为空
我不知道如何申报,具体在哪里申报?请帮忙。
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)