keyEvents freezes keys while other methods can call the same method properly in Java

109 views Asked by At

I'm experiencing a weird problem. After hours of trial and Error I've found the root of the problem but it does not make any sense to me.

I have a Main Class which has the following method:

public static void test(){
System.out.println("test Method is called");
}

I have a Keys Class. An object of the Keys is added to an object of Main.

Here is the Minimal Complete Verifiable Example: Main2 Class

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main2 extends JPanel {
private static Main2 main = new Main2();;

public static final Main2 main() {
    return main;
}

public static void test() {
    System.out.println("test Method is called");
}

public final JFrame frame = new JFrame();
public final Keys2 keys = new Keys2();

// ==============================================================

public static void main(String[] args) {}

private Main2() {
    System.out.println("START");
    Main2.main = this;
    frameInit();
    init();
    gameLoop();
}

private void init() {
    keys.start();
    //keys.left();  Removing // from this line partially solves the problem.
}

private void frameInit() {
    frame.add(main);
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

private void gameLoop() {
    while (true) {// Removing while loop solves the problem
 //Code here is omitted due to being irrelevant 
    }

}// loop end

}

Keys Class:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Keys2 implements KeyListener {
// Default keys will be able to be changed
    private int leftKey = 37; // left b.


public void start() {
    Main2.main().addKeyListener(this);
    Main2.main().setFocusable(true);

}

public boolean isPressed(int keyCode) {
    return false; // pressedKeys.contains(keyCode);
}

@Override
public void keyPressed(KeyEvent e) {
    // non-critical keys:
    System.out.println(e.getKeyCode());
     if (e.getKeyCode() == leftKey)
        left();
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {}

// ========================================================
// KEY METHODS

public void left() {
    // default left
    System.out.println("left");
    Main2.test();
    System.out.println("end");
}

}// END OF THE KEYS CLASS

Pressing the keys prints the values. So keys are working. Notice that I've changed the left()method to call the static test() method in the Main class. Right now as long as I don't press left button there is no problem but when I press Left I only get the following result in the console:

left

Neither "end" nor "test method is called" is printed. There is no error message. Also after pressing left button, all of the key methods stop working (game loop still works so program is not frozen). Mouse listener also stops working. I can't even close the JFrame window by clicking the X button.

However if I call the left() method from the object of the Main or another object or even from the start() method, it works with no errors and console gets the following message:

left
test Method is called
end

After that I can press the left button with no errors and get the same message.

What is the reason for this? How can I fix this?

Edit: I've replaced my code with a MCVE

1

There are 1 answers

2
WVrock On

I've fixed my problem by changing private static Main2 main = new Main2(); into private static Main2 main;

Then I've added main= new Main2(); into the main method. This fixed the problem. Why and how? I don't have the faintest idea.