JTable custom cell renderer focus problem

北城余情 提交于 2019-12-01 04:48:17

问题


I have a table like this. The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor.

Looks good. But the problem is We have to type the values and must hit "ENTER". In that image, I have typed my password but didn't hit Enter. So If I click the 'Save & Close' button, it'll show me an error that password field is empty.

Previously I have used only JTextFields and JPasswordFields under JTabbedPane and it worked well. When I had to add more and more stuff, I changed it to a table like this.

For now I have put a label to let people know that they should hit the ENTER.. This is not nice. Another big issue. Atleast in Nimbus Look and feel, we get an idea that that field is still in focus. In Windows system look, there's not much visible difference whether the field is focused or not.

I need the Username field or password field to set it's value when I click 'Save & Close' button. Please help me.


回答1:


So your problem is, that you are still editing the cell. So you have to stop the editing and then the cell will be changed.

At your button you can get the cell who is being edited with
TableCellEditor cellEditor = table.getCellEditor();
then you can stop the editing with
if(cellEditor!=null){
cellEditor.stopCellEditing();
}

and then you can save the value




回答2:


Tell the table to automatically commit when losing focus:

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


来源:https://stackoverflow.com/questions/7266800/jtable-custom-cell-renderer-focus-problem

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