java - javafx: How to bind the Enter key to a button and fire off an event when it is clicked? -


basically, have okaybutton sits in stage , when clicked , performs list of tasks. want bind enter key button such when clicked or enter key pressed, performs list of tasks.

    okaybutton.setonaction(e -> {                   .........         }     }); 

how can ? have read following post already. however, did not me achieve want do.

fist, set hanlder on button :

okaybutton.setonaction(e -> {               ...... }); 

if button has focus, pressing enter automatically call handler. otherwise, can in startmethod :

@override public void start(stage primarystage) {       // ...       node root = ...;       setglobaleventhandler(root);        scene scene = new scene(root, 0, 0);       primarystage.setscene(scene);       primarystage.show(); }  private void setglobaleventhandler(node root) {     root.addeventhandler(keyevent.key_pressed, ev -> {         if (ev.getcode() == keycode.enter) {            okaybutton.fire();            ev.consume();          }     }); } 

if have 1 button of kind, can use instead

okaybutton.setdefaultbutton(true); 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -