JavaFX 和 FXML - 使用来自另一个控制器的数据更新标签
JavaFX and FXML - update label with data from another controller
我有一个 window 带有标签和按钮,另一个 window 带有文本字段和按钮。从主 window 我想使用按钮打开另一个 window,在新 window 的文本字段中输入内容,然后单击新 window 上的按钮] 我希望它关闭并且主 window 标签更新为输入的文本。我也希望新的 window 是模态的。
Main Window
New Window
public class MainController {
@FXML
public void showNewWindow() {
try {
Stage newWindowStage = new Stage();
newWindowStage.setTitle("New Window");
newWindowStage.initModality(Modality.APPLICATION_MODAL);
VBox root = FXMLLoader.load(getClass().getResource("newWindow.fxml"));
Scene scene = new Scene(root);
newWindowStage.setScene(scene);
newWindowStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class NewWindowController {
@FXML
private TextField textField;
@FXML
public void updateMainWindowLabel() {
// update label in main window
// close new window
}
}
我知道它根本没有设置正确,但希望它能解释我正在尝试做的事情。
这是有效的示例代码(它不是最简洁的,所以我希望能修改)
我没有完全理解模态 windows 但 here 我发现了另一个与此相关的问题。
扩展应用程序的主要 Class:
public static void main(String[] args) {
Application.launch(Test.class, args);
}
private static Stage stage1;
private static Stage stage2;
private static MyExampleController controller;
@Override
public void start(Stage primaryStage) throws Exception {
stage1 = primaryStage;
FXMLLoader loader1 = new FXMLLoader(Test.class.getResource("/test/MyExample.fxml"));
AnchorPane pane1 = (AnchorPane)loader1.load();
controller = loader1.getController();
Scene scene1 = new Scene(pane1);
stage1.setScene(scene1);
stage2 = new Stage();
FXMLLoader loader2 = new FXMLLoader(Test.class.getResource("/test/MyEx2.fxml"));
AnchorPane pane2 = (AnchorPane)loader2.load();
Scene scene2 = new Scene(pane2);
stage2.setScene(scene2);
stage1.show();
}
public static Stage getStage2(){
return stage2;
}
public static MyExampleController getController(){
return controller;
}
控制器class1:
public class MyExampleController implements Initializable {
@FXML
private Label labLab;
@FXML
private Button btnButton;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void clickButton(ActionEvent event) {
Test.getStage2().show();
}
public void updateLabel(String newLabelText){
this.labLab.setText(newLabelText);
}
}
控制器class 2:
public class MyEx2Controller implements Initializable {
@FXML
private Button btnUpdate;
@FXML
private TextField txtField;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void doUpdateForTitle(ActionEvent event) {
Test.getController().updateLabel(txtField.getText());
Test.getStage2().close();
}
}
你需要2个阶段来做你想做的事(不仅仅是2个场景)。
Stage st
st.setOnCloseRequest(e -> {
});
将允许在您的舞台(您的新窗口)关闭时执行您想要的任何代码。
然后在 TextField 对象上使用 getText()
- 并在主舞台 Label 对象上使用 setText()
。
st.initModality(Modality.APPLICATION_MODAL);
...将使它成为模态。
我有一个 window 带有标签和按钮,另一个 window 带有文本字段和按钮。从主 window 我想使用按钮打开另一个 window,在新 window 的文本字段中输入内容,然后单击新 window 上的按钮] 我希望它关闭并且主 window 标签更新为输入的文本。我也希望新的 window 是模态的。
Main Window
New Window
public class MainController {
@FXML
public void showNewWindow() {
try {
Stage newWindowStage = new Stage();
newWindowStage.setTitle("New Window");
newWindowStage.initModality(Modality.APPLICATION_MODAL);
VBox root = FXMLLoader.load(getClass().getResource("newWindow.fxml"));
Scene scene = new Scene(root);
newWindowStage.setScene(scene);
newWindowStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class NewWindowController {
@FXML
private TextField textField;
@FXML
public void updateMainWindowLabel() {
// update label in main window
// close new window
}
}
我知道它根本没有设置正确,但希望它能解释我正在尝试做的事情。
这是有效的示例代码(它不是最简洁的,所以我希望能修改)
我没有完全理解模态 windows 但 here 我发现了另一个与此相关的问题。
扩展应用程序的主要 Class:
public static void main(String[] args) {
Application.launch(Test.class, args);
}
private static Stage stage1;
private static Stage stage2;
private static MyExampleController controller;
@Override
public void start(Stage primaryStage) throws Exception {
stage1 = primaryStage;
FXMLLoader loader1 = new FXMLLoader(Test.class.getResource("/test/MyExample.fxml"));
AnchorPane pane1 = (AnchorPane)loader1.load();
controller = loader1.getController();
Scene scene1 = new Scene(pane1);
stage1.setScene(scene1);
stage2 = new Stage();
FXMLLoader loader2 = new FXMLLoader(Test.class.getResource("/test/MyEx2.fxml"));
AnchorPane pane2 = (AnchorPane)loader2.load();
Scene scene2 = new Scene(pane2);
stage2.setScene(scene2);
stage1.show();
}
public static Stage getStage2(){
return stage2;
}
public static MyExampleController getController(){
return controller;
}
控制器class1:
public class MyExampleController implements Initializable {
@FXML
private Label labLab;
@FXML
private Button btnButton;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void clickButton(ActionEvent event) {
Test.getStage2().show();
}
public void updateLabel(String newLabelText){
this.labLab.setText(newLabelText);
}
}
控制器class 2:
public class MyEx2Controller implements Initializable {
@FXML
private Button btnUpdate;
@FXML
private TextField txtField;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void doUpdateForTitle(ActionEvent event) {
Test.getController().updateLabel(txtField.getText());
Test.getStage2().close();
}
}
你需要2个阶段来做你想做的事(不仅仅是2个场景)。
Stage st
st.setOnCloseRequest(e -> {
});
将允许在您的舞台(您的新窗口)关闭时执行您想要的任何代码。
然后在 TextField 对象上使用 getText()
- 并在主舞台 Label 对象上使用 setText()
。
st.initModality(Modality.APPLICATION_MODAL);
...将使它成为模态。