2

我有一个较旧的基于 Java swing 的独立应用程序,它使用 JFrame 和 JMenuBar,其中包含多个 Jmenu 元素(具有相应的 JMenuItem 项)。

在 Windows(7 和 vista)上升级到最新的 1.6.0_41(或 1.7.x)JVM 后,我注意到带有快捷键 Ctrl-C(或 Ctrl-Insert)的菜单项不再收到它的 ActionEvent,如果JTable 被添加到框架中。但是,如果通过鼠标单击访问菜单,则会调用菜单 ActionListener。如果删除了 JTable,则该快捷方式有效。如果我将快捷方式组合更改为 Ctrl-C 或 Ctrl-Insert(即 Ctrl-L)以外的其他内容,则会调用 ActionListener。

它过去的工作方式(我刚刚在 Windows Vista 上使用 jvm 1.4 确认了它 - 我知道该环境得到任何认真关注已经有一段时间了 :) 是 Ctrl-C 将执行标准复制到剪贴板功能如果焦点位于可编辑字段内,则为 JTable。否则,我的菜单 ActionListener 是通过 setAccelerator() 方法分配的快捷方式调用的。

看起来 JTable 实现在 1.6.* 中发生了变化,以在 Windows 上以不同方式处理 Ctrl-C 绑定事件。

在 Mac OS (JVM 1.6.0_43) 上运行这个应用程序我可以看到 ActionListener 是通过 Ctrl-C 快捷方式调用的。虽然可能是因为 JTable 在 Mac OS 下使用 Command-C 而不是 Ctrl-C 来复制到剪贴板。

我已经提取了演示问题的代码的相关部分。非常感谢任何建议。

public class TestFrame extends JFrame {

public TestFrame(String title) {

    super(title);
}

private void init() {

    getContentPane().setLayout(new BorderLayout());

    addMenu();
    addTable();

    // Change default exit operation
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    pack();
    setVisible(true);
}

private void addTable() {

    JTable jTable = new JTable(createTableModel());

    // Place table in JScrollPane
    JScrollPane scrollPane = new JScrollPane(jTable);

    // Add Table
    add(scrollPane, BorderLayout.CENTER);
}

private TableModel createTableModel() {

    Object[][] data = new Object[][]{ 
            {new Date(), "First Row, 2nd column", "First Row, 3rd column"},
            {new Date(), "Second Row, 2nd column", "Second Row, 3rd column"},
        };

    Object[] columnNames = new Object[]{"Date", "Type", "Description"};

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {

        public boolean isCellEditable(int row, int column) {
            return column != 0;
        }

    };

    return model;
}

private void addMenu() {

    // Create the menu bar.
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu editMenu = new JMenu("Edit");
    menuBar.add(editMenu);

    TestActionListener listener = new TestActionListener();
    JMenuItem menuItem = null;

    menuItem = new JMenuItem("Copy 1");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, ActionEvent.CTRL_MASK));
    menuItem.addActionListener(listener);
    editMenu.add(menuItem);

    menuItem = new JMenuItem("Copy 2");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    menuItem.addActionListener(listener);
    editMenu.add(menuItem);

    menuItem = new JMenuItem("Copy 3");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
    menuItem.addActionListener(listener);
    editMenu.add(menuItem);
}


public static void main(String[] args) {

    TestFrame frame = new TestFrame("Test");
    frame.init();
}


private static class TestActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        System.out.println("TestFrame.TestActionListener.actionPerformed(): e="+ e);
    }
}

}

4

2 回答 2

2

The problem is that your frame is not focused and there are no elements in your whole component hierarchy which has the focus, meaning that no one will "grab" the event and try to do something with it. Since JMenuItem's bind their shortcut to the input map JComponent.WHEN_IN_FOCUSED_WINDOW, your shortcut never "answers" the event.

To fix this, either put the focus on one of the component or directly on the JFrame (for example with frame.requestFocusInWindow();). Small example here:

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

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class TestFrame extends JFrame {

    public TestFrame(String title) {

        super(title);
    }

    private void init() {

        getContentPane().setLayout(new BorderLayout());

        addMenu();
        addTable();

        // Change default exit operation
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        pack();
        setVisible(true);
    }

    private void addTable() {

        JTable jTable = new JTable();

        // Place table in JScrollPane
        JScrollPane scrollPane = new JScrollPane(jTable);

        // Add Table
        add(scrollPane, BorderLayout.CENTER);
    }

    private void addMenu() {

        // Create the menu bar.
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu editMenu = new JMenu("Edit");
        menuBar.add(editMenu);

        TestActionListener listener = new TestActionListener();
        JMenuItem menuItem = null;

        menuItem = new JMenuItem("Copy 1");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, KeyEvent.CTRL_MASK));
        menuItem.addActionListener(listener);
        editMenu.add(menuItem);

        menuItem = new JMenuItem("Copy 2");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK));
        menuItem.addActionListener(listener);
        editMenu.add(menuItem);

        menuItem = new JMenuItem("Copy 3");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_MASK));
        menuItem.addActionListener(listener);
        editMenu.add(menuItem);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestFrame frame = new TestFrame("Test");
                frame.init();
                frame.requestFocusInWindow();
            }
        });
    }

    private static class TestActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("TestFrame.TestActionListener.actionPerformed(): e=" + e);
        }
    }
}

Additional remarks:

  • Don't extend JFrame if not needed
  • Start your UI from the Event Dispatching Thread (EDT) by using SwingUtilities.invokeLater()
于 2013-04-22T09:24:53.343 回答
1

如果您的问题是如何从表中删除 Control+C 绑定,那么您可以执行以下操作:

KeyStroke copy = KeyStroke.getKeyStroke("control C");
InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.getParent().remove(copy);

但是,这将从所有表中删除绑定。

于 2013-04-22T03:45:25.790 回答