转换FXML对话框的结果

时间:2016-06-06 19:39:10

标签: java javafx fxml

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

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

但现在我不想回归&#34; A&#34;所有的时间,但&#34; A&#34;如果&#34;评论&#34;被选中,&#34; B&#34;如果&#34;调查&#34;被选中等等。

我该怎么做?

2 个答案:

答案 0 :(得分:4)

所以你有一个控制器: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/backgrounds"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:background="@drawable/icon" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> ,你将定义myapp.AddDialogController。因此控制器知道与对话框中的单选按钮关联的切换组。

而不是使用静态FXMLLoader加载函数,而是创建一个新的FXMLLoader并使用(非静态)加载函数。完成后,您可以从加载器实例中获取对控制器的引用。从控制器获取控制器参考的技术在以下答案中进行了说明:Passing Parameters JavaFX FXML

获得对控制器的引用后,您可以将其作为附加参数传递给@FXML ToggleGroup #myToggleGroup函数。在该函数中,您可以改为从切换组中的选定切换(通过查询添加到Controller convertDialogResult的新方法找到)中的选定切换返回到字符串中而不是仅返回字符A希望(例如“评论”)。如果仅在所选切换按钮上调用getText()就足够了,您甚至不需要在代码中维护映射。

然而,最后,我认为首选的选项,而不是像你所做的那样从控制器外部化一些对话逻辑,而是封装控制器内部的一些逻辑,类似于下面的内容:

MyApp的/附加dialog.fxml

getSelectedToggle

的myapp / AddDialogController.java

<?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 / DialogDisplayApp.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();
    }
}

答案 1 :(得分:0)

您是否已经尝试过此操作:get-selected-radio-button-from-togglegroup

group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
    public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {

         if (group.getSelectedToggle() != null) {

             System.out.println(group.getSelectedToggle().getUserData().toString());
             // Do something here with the userData of newly selected radioButton

         }

     } 
});