Enter value in JTable cell and click OK does not register value

▼魔方 西西 提交于 2021-02-11 14:11:53

问题


I have a JTable which has certain cells editable. If the user enters in a value into an editable cell and then clicks the OK button on my form, the code:

// Get the value in the editable cell
String value = model.getValueAt(row, column); 

does not pick up the entered value.

The only way the entered value is picked up by the above code is once the user has pressed enter after typing in the value or clicking in another cell for the value to be "pushed in".

Does someone know what code I could write that would "push" the value in when they click the OK button?


回答1:


The JTable doesn't know when you are finished typing until you use "Enter" or tab to a new cell.

The value you are typing is not saved in the TableModel until you do one of the above.

A couple of solutions:

When you create the table you can use:

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

Or, in the ActionListener you can add:

if (table.isEditing())
    table.getCellEditor().stopCellEditing();

See Table Stop Editing for more information on above suggestions.




回答2:


Here is a simple code whice will help you.

if(jTable.isEditing){
       jTable.stopCellEditing();
    }


来源:https://stackoverflow.com/questions/5037816/enter-value-in-jtable-cell-and-click-ok-does-not-register-value

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