JAVA - Creating a KeyPress Event During a Button Action Listener click event?

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.

Up vote 1 down vote favorite share g+ share fb share tw.

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! Java keypress actionlistener keyevent link|improve this question asked Apr 15 '11 at 22:06Jay334.

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; } } }); } }.

Thanks for the suggestion. Just implemented it, works like a charm! – Jay Apr 15 '11 at 22:24.

As I've found, you can construct any new event and then send it through Component.processXxxEvent().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions