我可以用
ChoiceDialog<String> dialog = new ChoiceDialog();
dialog.showAndWait();
但是我只有一个选择框可供使用,我需要在一个对话框中使用3个。我怎么能这样做?
答案 0 :(得分:0)
以下是答案:
public void showAndWait(Window owner) throws IOException {
Dialog<String> dialog = new Dialog<>();
dialog.getDialogPane().setContent(FXMLLoader.load(getClass().getResource("/resources/customDialog.fxml")));
dialog.getDialogPane().setHeaderText("text");
ButtonType confirm = new ButtonType("ok", ButtonBar.ButtonData.OK_DONE);
ButtonType cancel = new ButtonType("cansel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(cancel, confirm);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.setTitle("title");
dialog.initOwner(owner);
dialog.showAndWait();
}
看起来不错,我使用这个方法制作类,并使用类似控制器的类到fxml,所以现在我可以轻松地使用任何控件而没有任何麻烦
答案 1 :(得分:0)
您可以使用http://code.makery.ch/blog/javafx-dialogs-official/
中的教程自行构建自定义对话框我调整了三个下拉菜单的自定义对话框;在我的情况下,合并两个自定义POJO相机对象与品牌,型号和序列号。请随意改编并使用下面的代码。
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
});