与已经打开的 FXML 控制器通信
Communicating with an FXML Controller that's already open
我找了好几遍都没找到。我有一个连接到控制器的 JavaFX FXML window;这个 window 是开放的。单击 window 上的按钮会触发另一个 FXML 文件的打开,链接到其各自的控制器。
第二个 window(optionsUI.fxml 和 optionsController)有几个单选按钮。单击其中一个时,我希望 image/button 在 mainUI window 中的位置发生变化。我该怎么做?
主控制器:
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 文件中存在的新值打开一个新阶段。
你可以在你的 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...
}
我找了好几遍都没找到。我有一个连接到控制器的 JavaFX FXML window;这个 window 是开放的。单击 window 上的按钮会触发另一个 FXML 文件的打开,链接到其各自的控制器。
第二个 window(optionsUI.fxml 和 optionsController)有几个单选按钮。单击其中一个时,我希望 image/button 在 mainUI window 中的位置发生变化。我该怎么做?
主控制器:
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 文件中存在的新值打开一个新阶段。
你可以在你的 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...
}