I'm having some troubles with cell editing in JTable.
First at all, I have created a function to handle horizontal scroll when ENTER is pressed and when it reach the last column, it start again over the first cell in the next column. This works good but the problem is that, when I click in a cell and make it editable then ENTER doesn't trigger the Action (I have to push it twice, once for stop editing and second one for start editing the next cell). It only recognize ENTER pressed when cell is selected (only the first time). After this, Action is always triggered when I continue pressing ENTER even cell is editable.
On the other hand, I have a cell listener for do some stuffs when some cells are edited, but it always is triggered twice when ENTER is pressed.
So, any suggestions? I hope I've explained this clearly.
datosTabla = new Object[10][columnas_tabla.length];
modelo = new DefaultTableModel(datosTabla, columnas_tabla);
tabla = new JTable(modelo);
CellEditorListener changeNotification = new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
}
public void editingStopped(ChangeEvent e) {
System.out.println("Do stuff");
}
};
tabla.getDefaultEditor(Object.class).addCellEditorListener(changeNotification);
Action handleEnter = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
int row = tabla.getSelectedRow();
int col = tabla.getSelectedColumn();
if (col < modelo.getColumnCount() - 1){
col++;
} else {
row++;
col = 0;
}
tabla.changeSelection(row, col, false, false);
tabla.editCellAt(row, col);
}
};
tabla.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "handleEnter");
tabla.getActionMap().put("handleEnter", handleEnter);