java - Converting the result of an FXML dialog -


i created simple dialog radio buttons

<?xml version="1.0" encoding="utf-8"?>  <?import java.net.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <?import javafx.scene.image.*?> <?import javafx.scene.image.image?>   <dialog fx:id="dialog"     fx:controller="myapp.adddialogcontroller"     xmlns:fx="http://javafx.com/fxml">     <dialogpane>         <dialogpane prefwidth="400.0" prefheight="300.0">             <stylesheets>                 <url value="@/css/styles.css" />             </stylesheets>             <content>                 <vbox>                     <fx:define>                         <togglegroup fx:id="mytogglegroup"/>                     </fx:define>                     <children>                         <radiobutton text="comment" togglegroup="$mytogglegroup"/>                         <radiobutton text="survey" togglegroup="$mytogglegroup"/>                         <radiobutton text="suggestion" togglegroup="$mytogglegroup"/>                     </children>                 </vbox>             </content>         </dialogpane>     </dialogpane> </dialog> 

and create this:

private string convertdialogresult(buttontype buttontype) {     if (buttontype == buttontype.ok) {         return "a";     } else {         return null;     } }  private dialog<string> createadddialog() throws applicationexception {      try {         dialog<nodeviewmodel> dialog = fxmlloader.load(getclass().getresource("/fxml/add_dialog.fxml"));          dialog.getdialogpane().getbuttontypes().addall(buttontype.ok, buttontype.cancel);         dialog.setresultconverter(this::convertdialogresult);          return dialog;     } catch (ioexception e) {         throw new applicationexception(e);     } } 

but i'd not return "a" time, "a" if "comment" selected, "b" if "survey" selected, etc.

how can it?

so have controller: myapp.adddialogcontroller , in have defined @fxml togglegroup #mytogglegroup. controller knows toggle group associated radio buttons in dialog.

instead of using static fxmlloader load function, instead create new fxmlloader , use (non-static) load function that. once that, can reference controller out of loader instance. technique getting controller reference controller demonstrated in answer to: passing parameters javafx fxml.

once have reference controller, can pass additional parameter convertdialogresult function. in function, instead of returning character a, can instead return mapping selected toggle in toggle group (which find out querying new method add controller getselectedtoggle) string wish (for instance "comment"). don't need maintain mapping in code if sufficient invoke gettext() on selected toggle button.


however, in end, think preferred option, rather externalizing of dialog logic controller have done, instead encapsulate of logic inside controller similar below:

myapp/add-dialog.fxml

<?xml version="1.0" encoding="utf-8"?>  <?import javafx.scene.control.*?> <?import javafx.scene.layout.vbox?>  <dialog fx:id="dialog"         fx:controller="myapp.adddialogcontroller"         xmlns:fx="http://javafx.com/fxml">   <dialogpane>     <dialogpane prefwidth="400.0" prefheight="300.0">       <content>         <vbox>           <fx:define>             <togglegroup fx:id="mytogglegroup"/>           </fx:define>           <radiobutton text="comment" togglegroup="$mytogglegroup"/>           <radiobutton text="survey" togglegroup="$mytogglegroup"/>           <radiobutton text="suggestion" togglegroup="$mytogglegroup"/>         </vbox>       </content>     </dialogpane>   </dialogpane> </dialog> 

myapp/adddialogcontroller.java

package myapp;  import javafx.beans.binding.bindings; import javafx.fxml.fxml; import javafx.scene.node; import javafx.scene.control.*;  public class adddialogcontroller {     @fxml private togglegroup mytogglegroup;     @fxml private dialog<string> dialog;      public void initialize() {         dialog.getdialogpane().getbuttontypes().addall(                 buttontype.ok, buttontype.cancel         );          node okbutton = dialog.getdialogpane().lookupbutton(buttontype.ok);         okbutton.disableproperty().bind(                 bindings.isnull(                         mytogglegroup.selectedtoggleproperty()                 )         );          dialog.setresultconverter(this::convertdialogresult);     }      private string convertdialogresult(buttontype buttontype) {         if (buttontype.ok.equals(buttontype)) {             return getselectedtogglevalue();         } else {             return null;         }     }      private string getselectedtogglevalue() {         radiobutton selectedradio = (radiobutton) mytogglegroup.getselectedtoggle();          if (selectedradio == null) {             return null;         }          return selectedradio.gettext();     } } 

myapp/dialogdisplayapp.java

import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.layout.vbox; import javafx.stage.stage;  import java.io.ioexception;  public class dialogdisplayapp extends application {      @override     public void start(stage stage) throws ioexception {         fxmlloader loader = new fxmlloader(                 getclass().getresource(                         "add-dialog.fxml"                 )         );          stage.setscene(new scene(new vbox(new label("main window")), 600, 400));         stage.show();          dialog<string> dialog = loader.load();          dialog.initowner(stage);         dialog.showandwait();          system.out.println(dialog.getresult());     }      public static void main(string[] args) {         launch(args);     } }