Accelerator commands not working when focus lost

时光总嘲笑我的痴心妄想 提交于 2019-12-12 18:56:47

问题


I have the following swing application,

which has a customized JMenuBar.

The code goes below,

public class MenuBarTest {
    public static void main(String[] args) {
        new JFXPanel();
        final JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.getContentPane().add(new JTextField("Testing", JLabel.CENTER),
                BorderLayout.CENTER);
        frame.setJMenuBar(new DummyMenuBar());
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

DummyMenuBar.java

public class DummyMenuBar extends JMenuBar {
private MenuBar menuBar;
private MenuItem menuItem;

public DummyMenuBar() {
    try {
        addToScene();
    } catch (Exception e) {
    }
}

private void addToScene() {
    final JFXPanel menuFxPanel = new JFXPanel();
    add(menuFxPanel);
    Platform.setImplicitExit(false);
    Platform.runLater(() -> {
        Scene scene = new Scene(new VBox(), 400, 20);
        initMenuItem();
        ((VBox) scene.getRoot()).getChildren().addAll(menuBar);
        menuFxPanel.setScene(scene);
    });
}

private void initMenuItem() {
    menuBar = new MenuBar();
    menuItem = new MenuItem("Item 1");
    menuItem.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN));
    Menu menu1 = new Menu("Dummy 1");
    menu1.getItems().add(menuItem);
    menuBar.getMenus().add(menu1);
    menuItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            System.out.println(".handle()");
        }
    });
}
}

The issue is, The accelerator commands (Ctrl+S) works only when the Menubar has focus.If i click on the JTextField(it gains focus) and accelerator commands doesn't work.

To make it work again i have to click on the MenuBar again to gain focus.

I can relate its because i have added JavaFX items over JmenuBar(DummyMenuBar.java) but not sure of exact root cause.

How can i solve this?


回答1:


I did a work around using KeyBinding,

public static void main(String[] args) {
        new JFXPanel();
        final JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        JTextField tf = new JTextField("Hello World");
        frame.getContentPane().add(tf,
                BorderLayout.CENTER);
        tf.getInputMap((JComponent.WHEN_FOCUSED)).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK), "Ctrl+S");
        tf.getActionMap().put("Ctrl+S", new SaveAction());
        frame.setJMenuBar(new DummyMenuBar());
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
        static class SaveAction extends AbstractAction {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            if((e.getSource() instanceof JTextField)){
            System.out.println(".handle() in Action");
            }
        }
    }

This Made sure , accelerator worked even when JFXPanel is out of focus.



来源:https://stackoverflow.com/questions/39588765/accelerator-commands-not-working-when-focus-lost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!