CellEditor.java 的 getCellEditorValue() 仅在单击单元格时调用

getCellEditorValue() of CellEditor.java is Only Called When the Cell is Clicked

考虑以下 Jtable,更准确地说是其中包含 JComboBox 的第一列:

当我尝试保存第一个 JComboBox(以“auth2”作为其值的那个)的值而不首先单击它时,当我检查数据库时,我发现一个空字符串。

但是,当我先点击它然后保存时,我得到了存储在数据库中的正确值。

使用调试器,我发现 CellEditor.java 的方法 getCellEditorValue() 只有在您首先单击 JComboBox 本身时才会被调用。

这解释了为什么在数据库中,当我首先单击 JComboBox 时我得到正确的值,而当我不单击它时,我得到一个空字符串。

所以我的问题是,有没有办法在我每次保存时调用 getCellEditorValue() 方法,无论我是否点击 JComboBox?

谢谢

I get the right value when I click on the JComboBox first and when I don't click on it, I get an empty String.

您不应尝试访问组合框中的值。组合框作为 table 中所有行的编辑器共享。

is there a way to call the getCellEditorValue() method every time I save

数据存储在 TableModel 中,而不是组合框中。

您可以随时使用 JTableTableModelgetValueAt(…) 方法从模型中获取值。

注意:数据可能尚未从编辑器保存到 TableModel(取决于您在做什么)。如果是这种情况,请查看: 解决方案。