Java: JPanel cannot receive key events after clicking a button (which does not have a registered event listener)?

First of all, thanks for taking the time to read my question, I appreciate that.

Here is an overview of what I have now:

I am writing a 3 by 3 slider puzzle using buttons in the form of grid cells. I added a JPanel key listener that contains these buttons and they respond perfectly to keyboard events.

However, the problem is somewhat peculiar, and I could not determine exactly why it exerts such unstable behavior. After clicking on one of these buttons, none of which are registered by event listeners, none of the buttons moves, but when you follow it with a key event, the keys stop responding.

My question is this: I know this is pretty vague, but does it call someone to be a Java problem, or does it sound like my error code behind all this?

I am really open to any suggestions, as it has been bothering me for about a week, and I still don’t know what caused the problem.

Again, thanks for taking the time to learn this.

@trashgod: yes of course.

public class Test2 extends JPanel{ JButton a = new JButton("A"); Test2(){ setFocusable(true); // Set layout to grid layout setLayout(new GridLayout(3, 3)); // Add button //a.setEnabled(false); add(a); // Register key event which shifts it to the next cell when the right arrow is pressed addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ if (e.getKeyCode() == KeyEvent.VK_LEFT) { remove(a); JButton b = new JButton("B"); //b.setEnabled(false); add(b); add(a); validate(); } } }); } 

}

Code Snippet Function: JPanel receives a key event, the left arrow to be exact, removes the "a" button and adds a new "b" button, and then the "a" button each time. However, if you try to run the program, after clicking the button, it simply stops listening to key events.

I was just thinking if this could have anything to do with the lost focus in JPanel? If so, what specific methods should I pay attention to restoring the lost focus?

Thanks!

(btw, setEnabled comments is my attempt to solve this problem by simply disabling the button, but it still does not explain how to restore lost focus on JPanel if this is a problem.)

+4
source share
2 answers

not all Keys are available for KeyListener , some of them are registered as built-in abbreviations for JComponents , it depends on the JComponent type and Look and Feel , if you want to listen to Keys from the keyboard, then you must implement KeyBindings , Swing JComponents are designed to use this listener, and KeyListener

working example for key A

 import java.awt.GridLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Test2 extends JPanel { private static final long serialVersionUID = 1L; private JButton a = new JButton("A"); public Test2() { setFocusable(true); setLayout(new GridLayout(3, 3)); a.setEnabled(false); add(a); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_A) { remove(a); JButton b = new JButton("B"); add(b); add(a); revalidate(); repaint(); } } }); } public static void main(String... args) { JFrame frame = new JFrame(""); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Test2()); frame.pack(); frame.setVisible(true); } } 
+1
source

We can focus on the JPanel by simply pressing setFocusable(false) on each button:

button1.setFocusable(false); or button2.setFocusable(false);

etc.

This will allow the JPanel, where keyListeners are added, to maintain focus.

+3
source

Source: https://habr.com/ru/post/1387834/


All Articles