无法获取正在编辑的单元格的当前值

时间:2014-01-03 19:08:53

标签: java swing jtable event-dispatch-thread tablecelleditor

我需要帮助,我在jtable更新中感到震惊。 我在截止日期:(

我有一个jtable和保存按钮。 table有两列,一列是标签,另一列是提供值。 输入的列包含文本字段和组合框。 当我点击保存时,表中的数据应该保存在文件的服务器端,同一个文件是ftp,来自该文件的数据显示在同一个jtable中。

问题是,当我编辑一个单元格,并且保持此单元格聚焦时,不会拾取该值。
如果我从该单元格中删除焦点(通过选择其他不可编辑的单元格),则更新该值。 />
经过谷歌搜索后,我找到了两个解决方案:
1. jtable.putClientProperty(“terminateEditOnFocusLost”,Boolean.TRUE);
2. jtable.getCellEditor()。stopCellEditing()

在调用saveData之前,我尝试了很多东西和上面解决方案的许多组合,但没有一个解决方案是永久性的。 以下解决方案80%的时间都在工作。

saveActionPerformed() {
    //-----------------
    if(jtable.isEditing()){
        jtable.getCellEditor().stopCellEditing();  
    } else {
        jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE)<br>
    }
    //-------------------
    saveData(); //save data on server side and ftp the file and display again on screen<br>
}

saveData() {
//should i put opposite functions of above used code in order to put back those properties on the jtable again, for example
    dislaydata();
    jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE)<br>
}

请告诉我可能的解决方案。 非常感谢你提前。 真诚地为压痕不良道歉。

1 个答案:

答案 0 :(得分:1)

jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE)

创建表时需要执行该语句:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE)

如果在ActionListener中调用该语句,则为时已晚,因为焦点已经在保存按钮上。