JMenuItem: how to set an Accelerators with 3 keys?

PeakGen picture PeakGen · May 19, 2012 · Viewed 9.6k times · Source

Please have a look at the following code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuActions extends JFrame
{
    private JMenuBar jmb;
    private JMenu file;
    private JMenuItem open;

    public MenuActions()
    {
        jmb = new JMenuBar();
        file = new JMenu("File");
        open = new JMenuItem("Open");
        open.addActionListener(new MenuAction());
        open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.VK_P,ActionEvent.CTRL_MASK));

        file.add(open);
        jmb.add(file);

        this.setJMenuBar(jmb);

        getContentPane().add(new JPanel());

        this.setSize(200,200);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private class MenuAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            JOptionPane.showMessageDialog(null,"OK");

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

In here, I need to fire the EventHandler of the JMenuItem when CTRL+O+P is pressed together, so it will display JOptionPane saying "OK". But as you can see, my attempt is giving an error! How can I do this when three of these keys are pressed together? Please help!

Answer

Yuriy Nakonechnyy picture Yuriy Nakonechnyy · May 19, 2012

It looks like you are using wrong version of KeyStroke.getKeyStroke() method - can't even find one taking 3 int parameters. Though, if you want you can use CTL + ALT + P instead of CTL + O + P

Try using this version: http://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.String)

like this: KeyStroke.getKeyStroke("control alt P")

Here try this code example :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuActions extends JFrame
{
    private JMenuBar jmb;
    private JMenu file;
    private JMenuItem open;

    public MenuActions()
    {
        jmb = new JMenuBar();
        file = new JMenu("File");
        open = new JMenuItem("Open");
        open.setAction(new MenuAction("Open", null, "Click to Open an Existing File.", KeyStroke.getKeyStroke("control alt P")));
        open.setAccelerator(KeyStroke.getKeyStroke("control alt P"));

        file.add(open);
        jmb.add(file);

        this.setJMenuBar(jmb);

        getContentPane().add(new JPanel());

        this.setSize(200,200);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private class MenuAction extends AbstractAction
    {
        public MenuAction(String title, ImageIcon image
                                        , String toolTipText
                                        , KeyStroke acceleratorKey)
        {
            super(title, image);
            putValue(SHORT_DESCRIPTION, toolTipText);
            putValue(SHORT_DESCRIPTION, toolTipText);
            putValue(ACCELERATOR_KEY, acceleratorKey);
        }
        public void actionPerformed(ActionEvent ae)
        {
            JOptionPane.showMessageDialog(null,"OK");
        }
    }
    public static void main(String[]args)
    {
        new MenuActions();
    }
}