英文:
CellEditorListener calls the getCellEditorValue method when focusgained for a column in jtable
问题
我有一个用于将编辑过的值存储到数据库中的方法。
public Object getCellEditorValue() {
    String outputToCell = ((JTextField) component).getText();
    panelsign.updateAllergicReactions(outputToCell, row);
    return outputToCell;
}
这个方法是被 JTable.class 中的以下方法调用(预定义的 Java 类)。
/**
 * 在编辑完成时调用。更改将被保存,编辑器将被丢弃。
 * <p>
 * 应用程序代码不会显式使用这些方法,它们在 JTable 内部使用。
 *
 * @param  e  接收到的事件
 * @see CellEditorListener
 */
public void editingStopped(ChangeEvent e) {
    // 接收新值
    TableCellEditor editor = getCellEditor();
    if (editor != null) {
        Object value = editor.getCellEditorValue();
        setValueAt(value, editingRow, editingColumn);
        removeEditor();
    }
}
如果我只是在 JTable 中点击行,就会调用第一个提到的方法。但是我希望在我完成编辑后再调用它。或者在编辑后导航到下一列来编辑下一个列时再调用它。
英文:
I have a method that is used to store the edited values in the DB.
public Object getCellEditorValue() {
	String outputToCell = ((JTextField) component).getText();
	panelsign.updateAllergicReactions(outputToCell, row);
	return outputToCell;
}
This method is called by this method in JTable.class(pre-defined java class).
/**
 * Invoked when editing is finished. The changes are saved and the
 * editor is discarded.
 * <p>
 * Application code will not use these methods explicitly, they
 * are used internally by JTable.
 *
 * @param  e  the event received
 * @see CellEditorListener
 */
public void editingStopped(ChangeEvent e) {
    // Take in the new value
    TableCellEditor editor = getCellEditor();
    if (editor != null) {
        Object value = editor.getCellEditorValue();
        setValueAt(value, editingRow, editingColumn);
        removeEditor();
    }
}
The first mentioned method is called if I just click the row in the JTable. But I want it to be called after I complete editing it. Or like after editing it and then navigate to next column to edit that one.
答案1
得分: 2
你不应该在单元格编辑器中进行操作。
如果你想知道何时在TableModel中更改了数据,可以选择以下方式之一:
- 覆盖你的TableModel的
setValueAt(...)方法以更新数据库,或者 - 向TableModel添加一个
TableModelListener。每当对TableModel进行更新时,都会生成一个事件。 
英文:
You should not be playing with the cell editor.
If you want to know when data have been changed in the TableModel you can either:
- Override the 
setValueAt(...)method of your TableModel to update the database, or - Add a 
TableModelListenerto theTableModel. An event will be generated whenever updates to the TableModel are made. 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论