JavaFx close window on pressing esc?


Question

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();
            }
        }
    });
1
6
1/16/2013 11:36:55 AM

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();
          }
        }
    });
12
11/13/2013 7:16:49 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon