与已经打开的FXML控制器进行通信

时间:2018-02-07 17:12:30

标签: java javafx fxml

我一次又一次地搜索这个无济于事。我有一个连接到Controller的JavaFX FXML窗口;这个窗口是开着的。单击窗口上的按钮会触发另一个FXML文件的打开,该文件链接到其各自的控制器。

第二个窗口(optionsUI.fxml和optionsController)有几个单选按钮。单击一个时,我希望在mainUI窗口中更改图像/按钮的位置。我该怎么做呢?

mainController:

public void assetPressed(MouseEvent event) {
        //Get the source of Handler
        HUDButton button = (HUDButton) event.getSource();

        //Check if an asset is already selected
        //----do a thing
            //Open stage
            openStage(currentAsset);

        } else {
            //if the current asset selected and the new asset clicked are the same
            //----do something
                closeStage();
            }
            //if the current asset selected and the new asset clicked are different
            else {
                //----do something else
                assetIsSelected = true;
                openStage(currentAsset);
            }
        }
    }
//opening optionsUI.fxml
public void openStage(Asset asset) {

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("optionsUI.fxml"));

        Parent root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        optionsController controller = fxmlLoader.getController();

        Scene scene = new Scene(root, 300, 450);
        stage.setScene(scene);
        if (alreadyExecuted == false) {
            stage.initStyle(StageStyle.UNDECORATED);
            stage.initOwner(stageControls); //Making the mainUI the owner of the optionsUI
            stage.setTitle("HUDEdit Version 3.0.0");
            alreadyExecuted = true;
        }

我遇到的主要问题是在单选按钮上添加一个事件处理程序,它将更改按下的按钮的属性(currentButton)。我搜索了这个问题,但我得到的就是我已经完成的工作:用另一个FXML文件中的新值打开一个新的阶段。

1 个答案:

答案 0 :(得分:1)

您可以在OptionsController中执行此类操作(我将重命名为符合标准naming conventions,顺便说一句。)

这里的基本想法是公开一个属性,表示用户通过单选按钮选择的内容。

public class OptionsController {

    @FXML
    private RadioButton radioButton1 ;

    @FXML
    private RadioButton radioButton2 ;

    private SomeType someValue1 = new SomeType();
    private SomeType someValue2 = new SomeType();

    private final ReadOnlyObjectWrapper<SomeType> selectedThing = new ReadOnlyObjectWrapper<>();

    public ReadOnlyObjectProperty<SomeType> selectedThingProperty() {
        return selectedThing.getReadOnlyProperty() ;
    }

    public final SomeType getSelectedThing() {
        return selectedThingProperty().get();
    }

    public void initialize() {
        radioButton1.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
            if (isNowSelected) {
                selectedThing.set(someValue1);
            }
        });
        radioButton2.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
            if (isNowSelected) {
                selectedThing.set(someValue2);
            }
        });
    }

    // ...
}

现在当您加载Options.fxml时,您可以观察该属性,并在值变化时执行您需要的任何操作:

public void openStage(Asset asset) {

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("optionsUI.fxml"));

    Parent root = null;
    try {
        root = fxmlLoader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
    OptionsController controller = fxmlLoader.getController();
    controller.selectedThingProperty().addListener((obs, oldSelection, newSelection) -> {
        // do whatever you need with newSelection....
    });

    // etc...
}