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 6224999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:45:53+00:00 2026-05-24T08:45:53+00:00

I have a java program with a JFrame and 3 JButtons in it. I

  • 0

I have a java program with a JFrame and 3 JButtons in it. I have added a keylistener to jframe. When i run the program a jframe window is opened and the first button is selected by default. My problem is that a KeyEvent is not being generated by this JFrame.
Now, besides adding a KeyListener to the jframe, i also added a KeyListener to the buttons.
Now the keyevent is being generated by the buttons.

How do I make the JFrame generate KeyEvent instead of the JButton generating them ??

Actually, my main purpose is building keyboard shortcuts for the buttons.

  • 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-05-24T08:45:56+00:00Added an answer on May 24, 2026 at 8:45 am

    Have a look here How to Use Key Bindings.
    An alternative to keylistener.

    Here is a little Example it has a Button with focus and process a KeyEvent (F2).
    On F2-clicked the Key-Binding process a ButtonClick which performed a System.out print.

    public class Example {
        static public void main( String[] s ) {
            EventQueue.invokeLater( new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.getContentPane().setLayout( new BorderLayout() );
                    frame.setBounds( 50, 50, 600, 600 );
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    final JButton button = new JButton( new AbstractAction("MyButton") {
                        @Override
                        public void actionPerformed( ActionEvent e ) {
                            System.out.println("Button Clicked");
                        }
                    });
                    frame.getContentPane().add( button );
                    frame.getRootPane().setDefaultButton( button );
    
                    KeyStroke f2 = KeyStroke.getKeyStroke("F2");
                    frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(f2, "clickButton");
                    frame.getRootPane().getActionMap().put("clickButton", new AbstractAction() {
                        @Override
                        public void actionPerformed( ActionEvent e ) {
                            button.doClick();
                        }
                    });
                    frame.setVisible( true );
                    // the Button has the focus
                    button.requestFocus();
                    // generate a KeyEvent 'F2' 
                    KeyboardFocusManager.getCurrentKeyboardFocusManager().dispatchKeyEvent( new KeyEvent( frame, KeyEvent.KEY_PRESSED, 0, f2.getModifiers(), f2.getKeyCode(), f2.getKeyChar() ) );
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Java program run as root on Unix, that therefore can read
In my Java app, I have a JFrame window, how can I minimize it
I currently have a small Java program which I would like to run both
I have to create a CD inventory program for my first Java class. The
I'd like to have ⌘ + W close a window/ JFrame in a program
I have a program with Driver GUI java file that creates a JFrame and
I have a problem with my Java program. It has a socket connection between
I have a Java program that loads thirdparty class files (classes I did not
We have a java program that requires a large amount of heap space -
I have a Java program that runs many small simulations. It runs a genetic

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.