Inconsistent behavior when editing JTable

user3750671

I want to create a JTable that has the following properties:

  • A single click in a cell will start editing (done - via setClickCountToStart(1))
  • Tabbing will take the user to the next cell (already the default)
  • Pressing enter will invoke some external method

I tried to implement this last item by adding an item to the ActionMap of the text field of a DefaultCellEditor. This works under the following sequence:

  • Click in a cell
  • Type some text
  • Click enter

But if I navigate to any other cell via tab, edit that cell, then click enter, this does not work. The Action is only fired if I hit enter in the cell that I started editing via a click. If I got to that cell by tabbing or using the arrow keys, it does not work.

I think maybe the focus is still on the cell where I started editing? I noticed that if I click/edit/tab a few times, then click in the currently edited cell before pressing enter, that seems to work.

Does tabbing take the user to the next cell without transferring focus? Any help would be appreciated.

package jtabletest;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.DefaultCellEditor;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class JTableKeyMap {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(JTableKeyMap::createAndDisplayGui);
    }


    public static void createAndDisplayGui() {
        final JTable table = new JTable(4, 4);
        final JTextField cell = new JTextField();
        final DefaultCellEditor cellEditor = new DefaultCellEditor(cell);
        cellEditor.setClickCountToStart(1);

        table.setDefaultEditor(Object.class, cellEditor);

        KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        String enterText = "enterKeyPressed";

        final InputMap iMap = cell.getInputMap(JComponent.WHEN_FOCUSED);
        iMap.put(enterKeyStroke, enterText);

        AbstractAction action = new AbstractAction() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                System.out.println("Callback for key event");
                cellEditor.stopCellEditing();
            }
        };

        ActionMap aMap = cell.getActionMap();
        aMap.put(enterText, action);

        aMap.put(KeyEvent.getKeyText(KeyEvent.VK_ENTER), new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Callback for key event");

                cellEditor.stopCellEditing();
            }
        });

        JScrollPane scrollPane = new JScrollPane(table);


        final JFrame frame = new JFrame("Table Test");
        frame.setContentPane(scrollPane);
        frame.setPreferredSize(scrollPane.getPreferredSize());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
camickr

Add the following when you create you JTable:

table.setSurrendersFocusOnKeystroke(true);

This will give the editor focus if you just start typing when a cell has focus. Then the editor will respond to your key bindings.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Text cursor is invisible when editing a JTable cell

Why is the behavior when assigning to values inconsistent?

Causes for inconsistent behavior when adding NaNs to a set

Java - Selenium - Inconsistent behavior when using Assert

Inconsistent behavior when assigning array to a property let

Inconsistent behavior when using await with dynamic type

Inconsistent Behavior in MS Word When Deleting Words

Groovy inconsistent behavior when assigning a string to an integer

Inconsistent behavior with setTimeout when Chrome DevTools is not open

Unstable plotly behavior when editing the hoverinfo text

Inconsistent Behavior

How to select all text in JTable cell when editing but not when typing?

Why is cancelCellEditing() not called when pressing escape while editing a JTable cell?

How to select all text in JTable cell when editing

JTable cell editing doesnt change when cell is validated

How to stop editing a JCombobox in a JTable when changing tab in a JTabbedPane

Inconsistent behavior when inserting a set into cells using .loc in pandas

Inconsistent behavior when replacing substring with tilde "~" in a BASH parameter expansion

localeCompare shows inconsistent behavior when sorting words with leading umlaut characters

Inconsistent behavior when updating a background image using JavaScript

How to deal with inconsistent behavior when change snapshot animatedDifferences value?

Passport.js `isAuthenticated()` inconsistent behavior; false when it should be true

Is there a reason there is inconsistent behavior when doing a search on an elastic index after it is reindexed?

Inconsistent behavior when trying to sort deep nested structures in Python

Inconsistent stringstream behavior when parsing doubles in libc++ and glibc

Inconsistent behavior when slicing a 2d array in PostgreSQL

Inconsistent behavior when parsing numbers from stringstream on different platforms

JTable : Unexpected behavior when clicking under the last item

Git Inconsistent behavior of "checkout"?