How do I get Integer in JTable cell to be selected when edit using keyboard
I have some cells in a JTable, when I double-click on the cell to edit it the current value (if any) is selected and is replaced by any value I enter, which is the behaviour I want. But when I use the keyboard to navigate to the cell and start editing, new values are added to the end of the current value which is not what I want.
I have created my own IntegerCellEditor (see below) and added a focus event or the textfield used when editing to select all the current text but it has no effect, any ideas please ?
publicclass IntegerCellEditorextends DefaultCellEditor
{
public IntegerCellEditor( JTextField textfield )
{
super( textfield );
//Ensure old value is always selected, when start editing
((JTextField)getComponent()).addFocusListener(new FocusAdapter()
{
publicvoid focusGained(FocusEvent fe){
((JTextField)getComponent()).selectAll();
}});
((JTextField)getComponent()).setHorizontalAlignment(JTextField.RIGHT);
}
/**
* Return as Integer (because delegate converts values to Strings).
*/
public Object getCellEditorValue()
{
return Integer.valueOf((String)delegate.getCellEditorValue());
}
}

