unable to fetch current value of cell being edited

穿精又带淫゛_ 提交于 2019-12-11 23:05:25

问题


I need help, i am struck with in jtable updation. I am on deadline :(

I have a jtable and save button. table has two columns, one column is labels and other column is for providing values. The column which takes input has text fields and combo boxes. When i click on save, the data from table should get saved on sever side in a file and same file is ftp and data from that file is displayed in same jtable.

The problem is when i edit a cell, and leave this cell focussed the value is not picked.
If i remove focus( by selecting other non editable cell) from that cell, the value is updated.

After googling a lot, i found two solutions:
1. jtable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
2. jtable.getCellEditor().stopCellEditing()

I tried many things and many combinations of above solutions before calling saveData but none of the solution is permanent. below solution is working 80% of the time.

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>
}

Please let me know the possible solution. A very big thank you in advance. Sincere apology for poor indentation.


回答1:


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

That statement needs to be executed when you create the table:

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

If you invoke that statement in your ActionListener it is too late because focus is already on the save button.



来源:https://stackoverflow.com/questions/20910983/unable-to-fetch-current-value-of-cell-being-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!