Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:22:00+00:00 2026-06-03T20:22:00+00:00

I want to trigger an save action anywhere in my application (Control+S). I’ve added

  • 0

I want to trigger an save action anywhere in my application (Control+S). I’ve added the necessary key binding, and the action triggers as expected. However, if I try Control+S on a JTable the table starts my custom action and activates the table cell for editing. I think I’ve disabled the edit action in the table’s input map. What am I missing here?

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

public class TestTableKeyBinding extends JFrame{

JTable table;
JScrollPane scroll;

public static void main(String[] args){
    TestTableKeyBinding test = new TestTableKeyBinding();
    test.setVisible(true);
}

TestTableKeyBinding(){
    super();
    initUI();
    addKeyBindings();
}

void initUI(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String[] headers = new String[]{"apples", "bananas"};
    String[][] data = new String[][]{{"1", "2"},{"4","6"}};
    table = new JTable(data, headers);
    table.setCellSelectionEnabled(true);
    scroll = new JScrollPane();
    scroll.setViewportView(table);
    this.add(scroll);
    this.pack();
    this.setSize(new Dimension(300, 400));  

}

void addKeyBindings(){
    //root maps
    InputMap im = this.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    ActionMap am = this.getRootPane().getActionMap();

    //add custom action
    im.put(KeyStroke.getKeyStroke("control S"), "save");
    am.put("save", saveAction());

    //disable table actions via 'none'
    table.getInputMap().put(KeyStroke.getKeyStroke("control S"), "none");
    table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control S"), "none");
    table.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("control S"), "none");
    table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control S"), "none");
    ((InputMap)UIManager.get("Table.ancestorInputMap")).put(KeyStroke.getKeyStroke("control S"), "none");
}

AbstractAction saveAction(){
    AbstractAction save = new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
               JOptionPane.showMessageDialog(TestTableKeyBinding.this.table, "Action Triggered.");
        }           
    };
        return save;
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:22:04+00:00Added an answer on June 3, 2026 at 8:22 pm

    Like @Guillaume, I had no problem running your code. You might look for a CellEditor that inadvertently defeats editingStopped(), discussed here. An sscce may help clarify the problem.

    Addendum: You can cancel editing in the saveAction() handler, as shown below.

    table.editingCanceled(null);
    

    For reference, I’ve updated your example in several respects:

    • Control-S is not bound to any JTable Action in common Look & Feel implementations, so there’s no need to remove it.
    • For cross-platform convenience, use getMenuShortcutKeyMask().
    • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    Code:

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.KeyStroke;
    
    public class TestTableKeyBinding extends JFrame {
    
        private static final int MASK =
            Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        private JTable table;
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TestTableKeyBinding test = new TestTableKeyBinding();
                    test.setVisible(true);
                }
            });
        }
    
        TestTableKeyBinding() {
            super();
            initUI();
            addKeyBindings();
        }
    
        private void initUI() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            String[] headers = new String[]{"apples", "bananas"};
            String[][] data = new String[][]{{"1", "2"}, {"4", "6"}};
            table = new JTable(data, headers);
            table.setCellSelectionEnabled(true);
            this.add(new JScrollPane(table));
            this.pack();
            this.setSize(new Dimension(300, 400));
    
        }
    
        private void addKeyBindings() {
            //root maps
            InputMap im = this.getRootPane().getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
            ActionMap am = this.getRootPane().getActionMap();
            //add custom action
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, MASK), "save");
            am.put("save", saveAction());
        }
    
        private AbstractAction saveAction() {
            AbstractAction save = new AbstractAction() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(
                        TestTableKeyBinding.this.table, "Action Triggered.");
                    table.editingCanceled(null);
                }
            };
            return save;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to trigger a special action in the save() method of a Django
I've got an after insert trigger that works well. However, I want it to
I want to trigger a javascript function (an Ajax Post save function) when someone
I want to automate enterprise deployment as a post-build action which triggers after archiving,
I want to trigger on click of a button this: <asp:Button runat=server ID=submit Text=Submit
I want to trigger javascript alert using PHP. Is it possible I want to
If I want to trigger an error in my interpreter I call this function:
I came in to scenario that I want to trigger service on specific time.
I have a div, containing text and a few links. I want to trigger
I want to create a trigger on a SQL Server table. Table has 8

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.