CellEditorListener在jtable的某一列获得焦点时调用getCellEditorValue方法。

huangapple go评论70阅读模式
英文:

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.
 * &lt;p&gt;
 * 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中更改了数据,可以选择以下方式之一:

  1. 覆盖你的TableModel的setValueAt(...)方法以更新数据库,或者
  2. 向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:

  1. Override the setValueAt(...) method of your TableModel to update the database, or
  2. Add a TableModelListener to the TableModel. An event will be generated whenever updates to the TableModel are made.

huangapple
  • 本文由 发表于 2020年9月9日 18:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63810178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定