Вопрос

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);
Это было полезно?

Решение

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

You are having issue with the Editor component's focus.

  1. When you double click on a cell using mouse, your cell editor receives the focus, any input using key press is reflected and pressing an Enter will be received by the editor component but not by the table.

  2. For the second case, when you are pressing Enter on a selected row(but the editor is yet to show), the Table itself listens to Enter press, shows the editor as you have coded, any key press is reflected on the editor and again when you click Enter, the table receives it, stop editing and goes to next cell.

As a solution, you can Declare a custom cell editor and implement ActionListener and actionPerformed function to respond with expected behavior. Check the demo example and tutorial page example for implementation ideas and details.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top