Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable custom cell renderer focus problem

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

enter image description here

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.

enter image description here

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

like image 444
Vigneshwaran Avatar asked Dec 13 '22 09:12

Vigneshwaran


2 Answers

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

like image 131
Neifen Avatar answered Dec 30 '22 21:12

Neifen


Tell the table to automatically commit when losing focus:

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
like image 38
kleopatra Avatar answered Dec 30 '22 21:12

kleopatra