@FXML
annotation. Make sure that the name
of the member control is equal to the fx:id
of the related control in Scene Builder. FxmlContentLoader
is responsible for
loading and initializing the dialog content. So do not specify the controller class in the content panel of the Scene Builder - FxmlContentLoader
takes care about.
The first parameter is the style class of the dialog and is required for content loading though CSS and also allows you to style all controls by CSS.
If not needed you can pass null
or simply do not specify a style class.
public class MyDialogController
{
@FXML protected TextField companyField;
@FXML protected TextField firstNameField;
@FXML protected TextField lastNameField;
@FXML protected TextField phoneField;
@FXML protected TextField faxField;
@FXML protected TextField emailField;
@FXML protected TextField tagsField;
@FXML protected TextArea infoField;
@FXML protected Button okButton;
@FXML protected Button cancelButton;
public Dialog<Person> createDialog(Window owner, String title)
{
Dialog<Person> dialog = new Dialog<Person>(owner, title, Modality.APPLICATION_MODAL, null, -1, -1);
RootPane rootPane = dialog.getRootPane();
new FxmlContentLoader("my-dialog", rootPane, this, ResourceBundle.getBundle("com/jyloo/syntheticafx/demo/resources/myDialog"))
{
@Override
public void init(Pane content, URL location, ResourceBundle resources)
{
new DialogAction(DialogActionType.OK).bindButton(okButton);
new DialogAction(DialogActionType.CANCEL).bindButton(cancelButton);
okButton.setDefaultButton(true);
dialog.setResultConverter(actionType -> {
if (actionType != DialogActionType.OK)
return null;
Person person = new Person();
person.company = companyField.getText();
person.firstName = firstNameField.getText();
person.lastName = lastNameField.getText();
person.phone = phoneField.getText();
person.fax = faxField.getText();
person.eMail = emailField.getText();
person.tags = tagsField.getText();
person.info = infoField.getText();
return person;
});
}
};
return dialog;
}
public static class Person
{
protected String company;
protected String firstName;
protected String lastName;
protected String phone;
protected String fax;
protected String eMail;
protected String tags;
protected String info;
@Override
public String toString()
{
return firstName + " " + lastName;
}
}
}
myDialog.properties
.
For more detailed instructions take a look at Oracle's documentation Internationalizing Your FXML Layout.
message.text=User information form.
company.text=Company:
firstName.text=First Name:
lastName.text=Last Name:
phone.text=Phone:
fax.text=Fax:
email.text=eMail:
tags.text=Tags:
info.text=Info:
The resource bundle file is passed to the FxmlContentLoader
. To support multiple languages provide multiple resource bundle files and append the related language id
to the file name i.e. myDialog_de.properties
for Germany. Note: the file extension doesn't have to be specified.
public class MyDialogController
{
...
public Dialog<Person> createDialog(Window owner, String title)
{
Dialog<Person> dialog = new Dialog<Person>(owner, title, Modality.APPLICATION_MODAL, null, -1, -1);
RootPane rootPane = dialog.getRootPane();
new FxmlContentLoader("my-dialog", rootPane, this, ResourceBundle.getBundle("com/jyloo/syntheticafx/demo/resources/myDialog"))
...
}
FXML
file which is used for the style class "my-dialog"
has to be specified by the CSS property -sfx-fxml-content
.
In the example below you can see the related style definition of the base CSS file and the final code of how the dialog will be created. The dialog returns a
simple plain Person
object. Specifying the content through CSS has some advantages as i.e. providing different forms for different operation systems
through pseudo classes.
.my-dialog{
-sfx-fxml-content: url("/com/jyloo/syntheticafx/demo/resources/fxml/form.fxml");
}
Dialog<MyDialogController.Person> d = new MyDialogController().createDialog((Window)null, "Person Dialog");
d.showAndWait();
System.out.println("Result: " + d.getResult());
It is also possible to specify the FXML
file directly by setting the content property for the root pane.
Dialog<MyDialogController.Person> d = new MyDialogController().createDialog((Window)null, "Person Dialog");
d.getRootPane().setStyle("-sfx-fxml-content: url(\"/com/jyloo/syntheticafx/demo/resources/fxml/form.fxml\");");
d.showAndWait();
System.out.println("Result: " + d.getResult());
Screenshot of the final resulting dialog.