I can use
ChoiceDialog<String> dialog = new ChoiceDialog();
dialog.showAndWait();
but i'll have only one choiceBox to use, and I need 3 of this in one dialog. How can i do this?
On
You can build yourself a custom dialog, using the tutorial at http://code.makery.ch/blog/javafx-dialogs-official/
I adapted their custom dialog for three dropdowns; in my case to merge two custom POJO camera objects with make, model and serial number. Feel free to adapt and use my code below.
Set<String> makes = new HashSet<>();
toMerge.stream().forEach((c) -> {
if (c.getMake()!=null && !c.getMake().isEmpty()) {
makes.add(c.getMake());
}
});
if (makes.isEmpty()) {
makes.add("UNKNOWN");
}
Set<String> models = new HashSet<>();
toMerge.stream().forEach((c) -> {
if (c.getModel()!=null && !c.getModel().isEmpty()) {
models.add(c.getModel());
}
});
if (models.isEmpty()) {
models.add("UNKNOWN");
}
Set<String> serials = new HashSet<>();
toMerge.stream().forEach((c) -> {
if (c.getSerial()!=null && !c.getSerial().isEmpty()) {
serials.add(c.getSerialNumber());
}
});
if (serials.isEmpty()) {
serials.add("UNKNOWN");
}
Dialog<HashMap<String, String>> dialog = new Dialog<>();
dialog.setTitle("Merge cameras");
dialog.setHeaderText("Please select the values for the merged camera. You can modify the merged camera later");
// Set the button types.
ButtonType mergeButtonType = new ButtonType("Merge", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(mergeButtonType, ButtonType.CANCEL);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
ComboBox<String> makesBox = new ComboBox<>();
makes.stream().forEach((make) -> {
makesBox.getItems().add(make);
});
makesBox.setValue(makes.iterator().next());
ComboBox<String> modelsBox = new ComboBox<>();
models.stream().forEach((model) -> {
modelsBox.getItems().add(model);
});
modelsBox.setValue(models.iterator().next());
ComboBox<String> serialsBox = new ComboBox<>();
serials.stream().forEach((serial) -> {
serialsBox.getItems().add(serial);
});
serialsBox.setValue(serials.iterator().next());
grid.add(new Label("Make:"), 0, 0);
grid.add(makesBox, 1, 0);
grid.add(new Label("Model:"), 0, 1);
grid.add(modelsBox, 1, 1);
grid.add(new Label("Serial number:"), 0, 2);
grid.add(serialsBox, 1, 2);
dialog.getDialogPane().setContent(grid);
// Convert the result to the desired data structure
dialog.setResultConverter(dialogButton -> {
if (dialogButton == mergeButtonType) {
HashMap<String, String> result = new HashMap<>();
result.put("make", makesBox.getValue());
result.put("model", modelsBox.getValue());
result.put("serial", serialsBox.getValue());
return result;
}
return null;
});
Optional<HashMap<String, String>> result = dialog.showAndWait();
result.ifPresent(r -> {
logger.debug("{}", r);
//TODO: handle result
});
Here is the answer:
Looks nice, i make class with this method, and use it class like controller to fxml, so now I can easily use any controls without any troubles