My problem is that I have a for loop and while this loop is running I still want inputs from JTextField to get read and not interrupt the loop or timing when the line gets printed.
public class test {
public void Play() {
JTextField textField = new JTextField();
textField.addKeyListener(new KeyChecker());
JFrame jframe = new JFrame();
jframe.add(textField);
jframe.setSize(200, 30);
jframe.setVisible(true);
}
}
class KeyChecker extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event)
{
for(int i = 0; i < 1; i = 0)
{
char Input = event.getKeyChar();
if(Input == 'a')
{
System.out.println("working");
} else {
System.out.println("not working");
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
}
In your code, the
keyPressedmethod gets called by the Event Dispatch Thread (https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) every time you press a key in the textfield.In this context the
KeyEvent eventparameter represents a sing key press event, so it makes no sense to create a loop inside an event method, as maloomeister correctly pointed out.Instead you should perform your logic using the state of your classes, either
KeyCheckerortest. I don't really understand why you should regularly check for a key being pressed (and the purpose of a textfield BTW), anyway the following is a possible solution using a separate thread. Of course you can also do the loop in the main thread (which is not the EDT), but in a non-trivial application you should never do that.