Start editing in a JTable cell with smart keys only

I think the JTable component should better handle the keys that will start editing. I mean, with the current implementation, if you type DEL, Ctrl + Shift + DEL, F5, F7, for example, the cell editor appears in the edited cell. In my opinion, start the cell editor with keys that are not very intuitive for the end user.

There is also another problem: JTable is not aware of other possible key bindings defined in the form. If you have the Ctrl + Shift + C key binding defined for the button in your form, if you type this key combination in your JTable, the table will start editing, and after that the action with the key binding will be called. I think there should be an easy way to prevent this, and not turn off all those already defined key bindings in the keyword binding mapping.

Is there any third-party component that has already solved, at least in part, some of these problems, especially the one that starts editing with a reasonable key? I would not want to do all the tedious filtering.

Any help would be greatly appreciated. Thank.

Marcos

UPDATE

As long as I use this extremely imperfect "solution", it at least makes things worse at the moment. Improvements, comments and suggestions are welcome.

    @Override
    public boolean isCellEditable(EventObject e)
    {
        if (e instanceof MouseEvent)
        {
            return ((MouseEvent) e).getClickCount() >=
                _delegate.getMouseClickCountToStartEditing();
        }
        else if (e instanceof KeyEvent)
        {
            KeyEvent event = (KeyEvent) e;

            int key = event.getKeyCode();
            if ((key >= KeyEvent.VK_F1 && key <= KeyEvent.VK_F12) &&
                KeyStroke.getKeyStrokeForEvent(event) != _startEditingKey)
            {
                return false;
            }

            int ctrlAlt = KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK;
            if ((event.getModifiersEx() & ctrlAlt) == ctrlAlt)
            {
                return true;
            }

            if ((event.getModifiersEx() & ctrlAlt) != 0)
            {
                return false;
            }

            return true;
        }
        else
        {
            // Is this else really needed? Are there other events types
            // other than mouse and key events?
            return true;
        }
    }

    // _startEditingKey is obtained with this method
    private KeyStroke getStartEditingKey()
    {
        InputMap bindings = TheTable.this.getInputMap(
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        for (KeyStroke key : bindings.allKeys())
        {
            Object binding = bindings.get(key);
            if ("startEditing".equals(binding))
            {
                return KeyStroke.getKeyStroke(
                    key.getKeyCode(), key.getModifiers(), true);
            }
        }
        return null;
    }
+5
source share
2 answers

You can implement a custom editor that cannot be edited if there are modifiers in keyEvent that you want to ignore:

DefaultCellEditor editor = new DefaultCellEditor(new JTextField()) {

    @Override
    public boolean isCellEditable(EventObject e) {
        if (e instanceof KeyEvent) {
            return startWithKeyEvent((KeyEvent) e);
        }
        return super.isCellEditable(e);
    }

    private boolean startWithKeyEvent(KeyEvent e) {
        // check modifiers as needed, this here is just a quick example ;-)
        if ((e.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0) {
            return false;
        }    
        // check for a list of function key strokes
        if (excludes.contains(KeyStroke.getKeyStrokeForEvent(e)) {
            return false;
        } 
        return true;
    }

};
JTable table = new JTable(new AncientSwingTeam());
table.setDefaultEditor(Object.class, editor);

: , ctrl-alt, DefaultEditorKit.DefaultKeyTypedAction: SunToolkit keyTyped ( keyPressed, ), , ( suntoolkit, , openjdk, )

+4

startEditing, . , . , @camickr.

+1

All Articles