3

我想在我的应用程序的任何地方触发保存操作(Control+S)。我已经添加了必要的键绑定,并且操作按预期触发。但是,如果我在 JTable 上尝试 Control+S,表格将启动我的自定义操作并激活表格单元格进行编辑。我想我已经禁用了表格输入映射中的编辑操作。我在这里想念什么?

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;
    }   
}
4

3 回答 3

4

像@Guillaume 一样,我运行您的代码没有问题。你可能会找一个CellEditor不经意间失败的,在这里editingStopped()讨论。sscce可能有助于澄清问题。

附录:您可以在saveAction()处理程序中取消编辑,如下所示。

table.editingCanceled(null);

作为参考,我在几个方面更新了您的示例:

代码:

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;
    }
}
于 2012-05-13T22:35:50.233 回答
2

我终于弄明白了。没有要设置的键绑定,操作映射不存在。所以 put 的调用没有做任何事情。

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");

JTable 通过触发表格来启用编辑,将按键事件传递给 editCellAt。有两种方法可以解决这个问题。1) 添加对 JTable 的输入/操作映射的操作引用或 2) 覆盖 editCellAt 以返回 false。这是我解决这个问题的方法。

public boolean editCellAt(int row, int column, EventObject e){              
            if(e instanceof KeyEvent){
                int i = ((KeyEvent) e).getModifiers();
                String s = KeyEvent.getModifiersExText(((KeyEvent) e).getModifiers());
                //any time Control is used, disable cell editing            
                if(i == InputEvent.CTRL_MASK){
                    return false;
                }
            }               
            return super.editCellAt(row, column, e);                
        }
于 2012-05-14T01:08:30.190 回答
0

我只是有一个类似的情况,我想重新分配左/右键。帮助我的是查看https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

最后我想出了这样的代码:

    {
        InputMap im = jTable1.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "decStash");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "incStash");
    }
    {
        InputMap im = jTable1.getInputMap(JComponent.WHEN_FOCUSED);
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "decStash");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "incStash");
    }
    {
        ActionMap am = jTable1.getActionMap();
        am.put("decStash", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                log.debug("decStash");
            }
        });
        am.put("incStash", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                log.debug("incStash");
            }
        });
    }

请注意,我的代码不会像所选答案那样查找根组件。

于 2021-12-13T07:15:10.980 回答