【发布时间】:2015-02-28 19:48:53
【问题描述】:
我有一个 JTable,它的单元格编辑器是 JTextField,我在 celleditor 上调用 focuslost 谁的代码是
public void focusLost(FocusEvent e)
{
JTextField textField = new JTextField();
textField = (JTextField) e.getSource();
textField.setText(getCurrencyEquivalent(textField.getText()));//read P.S
table.setValueAt(textField.getText(), table.getEditingRow(), 0);
}
我收到以下异常:
java.lang.ArrayIndexOutOfBoundsException: -1
当我调试时,很明显,在焦点实际上从 JTable 的单元格中丢失之后,在 cellEditor 上触发了焦点丢失。这使得 table.getEditingRow() 总是返回 -1,因此在 focuslost 处动态设置值是非常不可能的。如果可用,请帮助我通过任何其他技术解决此问题。
P.S: getCurrencyEquivalent() 是一种方法,它以不同的字符串格式返回给定字符串的格式化版本。
【问题讨论】:
-
你为什么要这样做?
JTableAPI/CellEditor应该为你处理这个问题,你应该调用stopCellEditing或cancelCellEditing
标签: java swing jtable jtextfield tablecelleditor