gpt4 book ai didi

java - 转换 FXML 对话框的结果

转载 作者:行者123 更新时间:2023-11-30 06:53:21 25 4
gpt4 key购买 nike

我创建了一个带有单选按钮的简单对话框

<?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>

我这样创建它:

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);
}
}

但现在我不想一直返回“A”,而是在选择“评论”时返回“A”,在选择“调查”时返回“B”,等等。

我该怎么做?

最佳答案

所以您有一个 Controller :myapp.AddDialogController,您将在其中定义@FXML ToggleGroup #myToggleGroup。因此 Controller 知道与对话框中的单选按钮关联的切换组。

与其使用静态 FXMLLoader 加载函数,不如创建一个新的 FXMLLoader 并使用其中的(非静态)加载函数。完成后,您可以从加载程序实例中获取对 Controller 的引用。从 Controller 获取 Controller 引用的技术在以下答案中进行了演示:Passing Parameters JavaFX FXML .

一旦获得对 Controller 的引用,就可以将其作为附加参数传递给 convertDialogResult 函数。在该函数中,您不仅可以返回字符 A,还可以从切换组中的选定切换返回一个映射(您可以通过查询添加到 Controller getSelectedToggle 的新方法找到)到你想要的字符串(例如“评论”)。如果只需在选定的切换按钮上调用 getText() 就足够了,您甚至不需要在代码中维护映射。


但是,最后,我认为首选的选择不是像您所做的那样从 Controller 中外部化一些对话逻辑,而是将一些逻辑封装在 Controller 内部,如下所示:

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);
}
}

关于java - 转换 FXML 对话框的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com