Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:22:43+00:00 2026-06-04T04:22:43+00:00

Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:22:44+00:00Added an answer on June 4, 2026 at 4:22 am

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
am new here. i have a slight problem; PLease look at the following code
Please have a look at the code below: import string from collections import defaultdict
Please have a look at the following code, which I have run in VB6
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
Please have a look a the following code sample, executed on a Windows-32 system
Please have a look at the following code: <HTML> <HEAD> <script src=//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js type=text/javascript></script> <SCRIPT
please have a look at the following code: (I removed all doctypes etc for
i was dealing with the following code,& got confused,please have a look at it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.