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

The Archive Base Latest Questions

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

I am creating a java Sudoku GUI application at the moment. The grid for

  • 0

I am creating a java Sudoku GUI application at the moment.

The grid for showing the Sudoku puzzle is simply a 2 dimensional array of myJButtons(implementing JButton) – for this problem they can be treated as regular JButtons.

The program will allow a button in the grid to be clicked, calling an actionlistener.

Is there a way to allow for a KeyAdapter Keypress to be created when a button is clicked to allow for a number press – physical key 1,2,3,4,5,6,7,8,9,0

I would like the action listener to work only for when a button is clicked.

A simpler example of this would be a Frame with a single button. when the button is pressed, the user can press a physical key on the keyboard, setting the jbutton text to the key value. Additional key presses would not change the button text, unless the button is clicked first.

class ClickAction implements ActionListener { // Action Listener called when button is Pressed

    public void actionPerformed(ActionEvent ae) {

        //need a way to create a keyevent listener here

    }
}

Thanks In advance to anyone who can answer this!

  • 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-21T08:49:54+00:00Added an answer on May 21, 2026 at 8:49 am

    I would use another solution. Instead of creating KeyEvent Listeners every time a Button is clicked, you could register the key listener during the start up of the application. Then you can use a flag to check if the button was clicked first. Only if this flag is true, you perform the actions in the KeyEvent listener. Otherwise you skip all statements in the KeyEvent Listener.

    Here an example:

    public class TestClass {
    
        private boolean isButtonClicked = false;
    
        public void testYourButtons() {
    
            JButton myButton = new JButton();
            myButton.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    isButtonClicked = !isButtonClicked;
                }
            });
    
            myButton.addKeyListener(new KeyListener() {
    
                @Override
                public void keyTyped(KeyEvent arg0) {
                    // TODO Auto-generated method stub  
                }
    
                @Override
                public void keyReleased(KeyEvent arg0) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void keyPressed(KeyEvent arg0) {
                    if (isButtonClicked) {
                        // TODO Do here your event handling
                        isButtonClicked = false;
                    }
                }
            });
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently creating a java application in which I have a 2d array
I've created a 2 dimensional 'grid' for a game world I'm creating in Java.
I've creating a Java Swing application and I realized that I have many many
I'm creating a Java application where the user can search through a list of
I am creating a java application with RestFB. At this url, Facebook explains how
I'm creating a Java desktop application and was wondering how I should deal with
I am creating a Java ME application to connect to server. My problem in
I am creating a Java EE web service application. I am not sure on
I am creating a Java program that uses a GUI to display a mortgage
I am creating my java Swing application in Netbeans. At present there are more

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.