Let me introduce the scenario of my problem. I wish to present the content of a database table in a JTable. With this JTable I should be able to insert new rows, delete rows, and update the content of the fields of the existing rows.
The first desired behavior is that when a cell gets the focus, if it is editable, it enters in edit mode directly with all their content selected if its an alphanumeric content. (Text, numbers, dates, etc.)
The next desired behavior is that the Enter key works as the Tab key, ie, pressing the Enter key focus must be transferred to the next cell (and if this is editable then enter edit mode) both forward (left to right) or backward.
To address the first requirement, I overwrite the changeSelection method of the JTable class with the following method.
@Override
public void changeSelection(int row, int column, boolean toggle, boolean extend) {
super.changeSelection(row, column, toggle, extend);
if (editCellAt(row, column)) {
Component editor = getEditorComponent();
editor.requestFocusInWindow();
if (editor instanceof JFormattedTextField) {
((JFormattedTextField) editor).select(0,
((JFormattedTextField) editor).getText().length());
} else if (editor instanceof JTextField) {
((JTextField) editor).selectAll();
}
}
}
After reading a lot of documentation and posts, it became clear that the most appropriate way to address the problem was through the use of key-bindings, basically and after all read, the solution was to assign the behavior of the Tab key to the Enter key, and so I did.
private void tableConfiguration() {
//Configuramos la tabla para que en el caso de que pierda el foco finalice la edición
putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
//Cambiamos el comportamiento por defecto de la tecla enter para que actue como TAB
getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "selectNextColumnCell");
// cambiamos Shift+Enter para que se comporte como Shift+Tab
getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK),
"selectPreviousColumnCell");
//configuramos el comportamiento por defecto que queremos que tenga nuestro grid
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//seleccion simple de celda
setCellSelectionEnabled(true);//muestra la celda activa seleccionada
getTableHeader().setReorderingAllowed(false);//no permite reordenar por columnas
setRowHeight(26);//altura de la fila
}
As you can see in the code section, they were assigned to Enter and Shift+Enter the behaviors of Tab the Shift+Tab keys.
The problem I have is that the Enter key has an unexpected behavior. When the cell takes focus, enters directly into edit mode, when I press the Enter key it finishes the editing but it doesn't transfer the focus to the next cell, I need to press again the Enter key to get that. Tab and Shift+Tab keys works as expected and curiously, Shift+Enter keys works well too, finishing editing, moving to the previous cell and starting in edit mode.
I have tried to correct that behavior following different strategies, overriding the method editingStopped of the JTable class, through the TableCellEditor class, using Listeners in diferent ways, etc, and I have not been able to correct that behavior so I'm stuck now. Anyone have a suggestions or the solution? what am I doing wrong?
Greetings,