4

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.

Vigneshwaran
  • 3,265
  • 6
  • 23
  • 36

2 Answers2

6

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

Neifen
  • 2,546
  • 3
  • 19
  • 31
  • The first column is not editable. So have to check for null before stopEditing(); Thank you very much for the simple solution. :) – Vigneshwaran Sep 01 '11 at 07:24
  • possible, but has drawbacks: a) you have to do it at _every_ location that relies on the commit having happened b) those locations have to know the JTable, leading to unwanted coupling – kleopatra Sep 02 '11 at 07:58
6

Tell the table to automatically commit when losing focus:

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
kleopatra
  • 51,061
  • 28
  • 99
  • 211