Accelerator on JMenuItem doesn't work if the JMenu is not open (but only a few)

176 views Asked by At

My problem is about accelerators on JMenuItem, the accelerator doesn't work if the JMenu is not opened, but only for some of them. For one it is working (CTRL + G), but for another one it is not working (CTRL + H). I have no idea why it doesn't work. (it's working if the JMenu is open I remind it)

Maybe it's about another shortcut already set up on that combination ?

remplacer.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_DOWN_MASK));
    remplacer.setText("Remplacer");
    remplacer.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            remplacerActionPerformed(evt);
        }
    });
    edition_menu.add(remplacer);

    atteindre.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_DOWN_MASK));
    atteindre.setText("Atteindre");
    atteindre.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            atteindreActionPerformed(evt);
        }
    });
    edition_menu.add(atteindre);

Here are two JMenuItem contained in a JMenu, one is working anyway, but the other is working only if I open the JMenu before. Thank's for your help.

1

There are 1 answers

0
MadProgrammer On

Seems to work fine for me, consider providing a minimal reproducible example which demonstrates your issue

import java.awt.Dimension;
import java.awt.EventQueue;
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.JPanel;
import javax.swing.KeyStroke;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JMenuItem replaceMI = new JMenuItem("Replace");
                replaceMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_DOWN_MASK));
                replaceMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Do replace");
                    }
                });
                JMenuItem findMI = new JMenuItem("Find");
                findMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.CTRL_DOWN_MASK));
                findMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Do find");
                    }
                });

                JMenu editMenu = new JMenu("Edit");
                editMenu.add(replaceMI);
                editMenu.add(findMI);

                JMenuBar menuBar = new JMenuBar();
                menuBar.add(editMenu);

                JFrame frame = new JFrame();
                frame.setJMenuBar(menuBar);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
}