JavaFX - 在窗口(阶段)之间传递值

时间:2015-10-26 08:02:17

标签: javafx-8

我有一个JavaFX应用程序,其中我有一个主窗口(阶段),用户应该从该窗口打开第二个子窗口。在第二个窗口中,使用时应该做一些会影响应用程序行为的事情。

在用户关闭此屏幕后,我无法理解如何访问第二个屏幕的数据/值。

这是类似情况的一个例子。假设我想得到第二阶段的文本框值。

package multiplestagesexample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 *
 * @author IdoG
 */
public class MultipleStagesExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("New Stage");        
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                new CreateStage();
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Main Stage");
        primaryStage.setScene(scene);
        primaryStage.show();

        // Get the TextField value from the Additional Stage ???
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}

class CreateStage {
    public CreateStage() {
        TextField textBox = new TextField();        

        StackPane root = new StackPane();
        root.getChildren().add(textBox);        

        Scene scene = new Scene(root, 300, 250);

        Stage stage = new Stage();
        stage.setTitle("Additional Stage");
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.showAndWait();
    }
}

这是相关“真实”代码的一部分:

@FXML
private void handleOpenMappingToolWindowAction(ActionEvent event) throws IOException {
    //mappingWindow is an instance variable
    //Stage mappingWindow; 

    // gets a Sheet object that will be used in the "MappingToolWindow" stage
    selectedSheet = (Sheet) sheetsBox.getSelectionModel().getSelectedItem(); 

    FXMLLoader loader = new FXMLLoader(getClass().getResource("MappingToolWindow.fxml"));
    Parent root = (Parent)loader.load();
    MappingToolWindowController controller = loader.getController();

    if (mappingWindow == null) {
        Scene mappingScene = new Scene(root);
        mappingWindow = new Stage();

        mappingWindow.initModality(Modality.APPLICATION_MODAL);
        mappingWindow.setTitle("Mapping Tool");
        mappingWindow.setScene(mappingScene);      

        controller.setSheet(selectedSheet);
    }

    mappingWindow.showAndWait();

    // ACCESS THE NEW STAGE ???
}

2 个答案:

答案 0 :(得分:4)

使文本字段成为实例变量,以便您可以定义访问其文本的方法:

class CreateStage {

    private TextField textBox ;

    public CreateStage() {
        textBox = new TextField();        

        StackPane root = new StackPane();
        root.getChildren().add(textBox);        

        Scene scene = new Scene(root, 300, 250);

        Stage stage = new Stage();
        stage.setTitle("Additional Stage");
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.showAndWait();
    }

    public String getText() {
        return textBox.getText();
    }
}

当然,你可以做到

CreateStage dialog = new CreateStage() ;
System.out.println(dialog.getText()); 

在你的处理程序中。

答案 1 :(得分:0)

public class XXXX extends Window {
    private static Stage popupstage;
    private Person aperson;
    public XXXX (Person a) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("../UserInterface/UI_XXXXX.fxml"));
        popupstage = new Stage();
        popupstage.setTitle(a.getAbc());
        popupstage.initModality(Modality.APPLICATION_MODAL);
        popupstage.setScene(new Scene(root,400,400));
    }
    public Stage getPopupstage() {
        return popupstage;
    }
}

有一个带参数的窗口。然后,您的窗口将读取传递的对象。在上面的示例中,将读取person对象并填充舞台的标题(新窗口)。