How to assign a shortcut (key combination) to a JavaFx dialog -


i have not found direct way assign keyboard shortcut button type in javafx 8 dialog.

for instance:

dialog.dialogpane().getbuttontypes.addall(buttontype.ok, buttontype.cancel) 

how can assign esc key buttontype.cancel ?

thanks.

you can use accelerator demonstrated in:

javafx dialogs bit weird (and complicated) in way define , use buttons, adding accelerator gets little convoluted, still possible. code below sets accelerator fire button action when key combination pressed, shortcut+f, shortcut key change depending on os (on os x key labeled "command").

// create custom button type: buttontype fishingbuttontype = new buttontype( "go fishing", buttonbar.buttondata.other);  dialog.getdialogpane().getbuttontypes().add(fishingbuttontype);  // set accelerator , action fishing button. button fishingbutton = (button) dialog.getdialogpane().lookupbutton(fishingbuttontype); fishingbutton.addeventfilter(actionevent.action, ae -> {     // linkware image: http://www.iconka.com     dialog.setgraphic(new imageview("http://icons.iconarchive.com/icons/iconka/meow/64/cat-fish-icon.png"));     ae.consume();  // consume action dialog not close when button pressed. }); fishingbutton.sceneproperty().addlistener((observable, oldvalue, newscene) -> {     if (newscene != null) {         newscene.getaccelerators().put(                 new keycodecombination(keycode.f, keycombination.shortcut_down),                 fishingbutton::fire         );     } }); 

executable sample code

most of code lifted excellent makery javafx dialog tutorial. additional code added tutorial code add fishing button , accelerator trigger it.

dialog before accelerator triggered: no fish

dialog after accelerator triggered: go fish

import javafx.application.application; import javafx.application.platform; import javafx.event.actionevent; import javafx.geometry.insets; import javafx.scene.node; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.image.imageview; import javafx.scene.input.*; import javafx.scene.layout.*; import javafx.stage.stage; import javafx.util.pair;  import java.util.optional;  public class logindialog extends application {      @override      public void start(stage stage) throws exception {         button showdialogbutton = new button("show dialog");         showdialogbutton.setonaction(event -> {             showdialog();         });          stackpane layout = new stackpane(showdialogbutton);         layout.setpadding(new insets(10));         stage.setscene(new scene(layout));          stage.show();     }      private void showdialog() {         // create custom dialog.         dialog<pair<string, string>> dialog = new dialog<>();         dialog.settitle("login dialog");         dialog.setheadertext("look, custom login dialog");          // set icon.         // icon source: http://www.iconarchive.com/show/soft-scraps-icons-by-hopstarter/lock-lock-icon.html         dialog.setgraphic(new imageview("http://icons.iconarchive.com/icons/hopstarter/soft-scraps/64/lock-lock-icon.png"));          // set button types.         buttontype loginbuttontype = new buttontype("login", buttonbar.buttondata.ok_done);         buttontype fishingbuttontype = new buttontype( "go fishing", buttonbar.buttondata.other);         dialog.getdialogpane().getbuttontypes().addall(loginbuttontype, fishingbuttontype, buttontype.cancel);          // create username , password labels , fields.         gridpane grid = new gridpane();         grid.sethgap(10);         grid.setvgap(10);         grid.setpadding(new insets(20, 150, 10, 10));          textfield username = new textfield();         username.setprompttext("username");         passwordfield password = new passwordfield();         password.setprompttext("password");          grid.add(new label("username:"), 0, 0);         grid.add(username, 1, 0);         grid.add(new label("password:"), 0, 1);         grid.add(password, 1, 1);          // enable/disable login button depending on whether username entered.         node loginbutton = dialog.getdialogpane().lookupbutton(loginbuttontype);         loginbutton.setdisable(true);          // set accelerator , action fishing button.         button fishingbutton = (button) dialog.getdialogpane().lookupbutton(fishingbuttontype);         fishingbutton.addeventfilter(actionevent.action, ae -> {             // linkware image: http://www.iconka.com             dialog.setgraphic(new imageview("http://icons.iconarchive.com/icons/iconka/meow/64/cat-fish-icon.png"));             ae.consume();         });         fishingbutton.sceneproperty().addlistener((observable, oldvalue, newscene) -> {             if (newscene != null) {                 newscene.getaccelerators().put(                         new keycodecombination(keycode.f, keycombination.shortcut_down),                         fishingbutton::fire                 );             }         });          // validation (using java 8 lambda syntax).         username.textproperty().addlistener((observable, oldvalue, newvalue) -> {             loginbutton.setdisable(newvalue.trim().isempty());         });          dialog.getdialogpane().setcontent(grid);          // request focus on username field default.         platform.runlater(username::requestfocus);          // convert result username-password-pair when login button clicked.         dialog.setresultconverter(dialogbutton -> {             if (dialogbutton == loginbuttontype) {                 return new pair<>(username.gettext(), password.gettext());             }             return null;         });          optional<pair<string, string>> result = dialog.showandwait();          result.ifpresent(usernamepassword -> {             system.out.println("username=" + usernamepassword.getkey() + ", password=" + usernamepassword.getvalue());         });     }      public static void main(string[] args) {         launch(args);     } } 

Comments