JTable密钥绑定 [英] JTable Key Bindings

查看:113
本文介绍了JTable密钥绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

赞@Guillaume,我运行你的代码没有问题。您可能会找到 CellEditor ,无意中击败 editingStopped(),讨论了此处 sscce 可能有助于澄清问题。

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.

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

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.

代码:

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

这篇关于JTable密钥绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆