Im calling a new stage in my program which I like to close on pressing escape. I did this which gives me a NullPointerException:
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
KeyCode key = t.getCode();
if (key == KeyCode.ESCAPE){
stage.close();
}
}
});
try this..
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>
() {
@Override
public void handle(KeyEvent t) {
if(t.getCode()==KeyCode.ESCAPE)
{
System.out.println("click on escape");
Stage sb = (Stage)label.getScene().getWindow();//use any one object
sb.close();
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With