JavaFX - 如何在另一个控制器的控制器中使用方法?

时间:2015-04-15 01:01:15

标签: java javafx controller javafx-8 scenebuilder

使用SceneBuilder。我有两个阶段,每个阶段都有一个控制器:
stage1Controllerstage2Controller Stage1Controller:

public class Stage1Controller {
    @FXML
    private MenuItem translate;
    @FXML
    private Menu file;
    @FXML
    private Menu edit;
    @FXML
    private Menu help;


    @FXML 
    private void handleTranslate (ActionEvent event){
        translateFirstStage();
        //HOW TO ACCESS THE stage2Controller setLabel()??
    }

    private void translateFirstStage(){
        file.setText("Fichier");
        edit.setText("Modifier");
        help.setText("Aide");
    }
}

Stage2Controller:

public class Stage2Controller {
    @FXML
    private Label lb;


    private void setLabel(String string){
        lb.setText("string");
    }
}

以下是使用2种方法(在Start(Stage primaryStage)方法中调用)在Main.java类中加载两个fxml文件的方法:

public void firstStage() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/stage1.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void secondStage() {

        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/stage2.fxml"));
            XD = (AnchorPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(XD);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • handleTranslate(ActionEvent event)方法用作第一阶段中OnAction的{​​{1}}方法,它会在两个阶段翻译视图。

如何将MenuItem translate放入setLabel方法?感谢

1 个答案:

答案 0 :(得分:10)

"快速而肮脏"方法是为Stage1Controller提供对Stage2Controller

的引用
public class Stage1Controller {

    private final Stage2Controller stage2Controller ;

    public void setStage2Controller(Stage2Controller stage2Controller) {
        this.stage2Controller = stage2Controller ;
    }

    // ...

    @FXML 
    private void handleTranslate (ActionEvent event){
        translateFirstStage();
        stage2Controller.setLabel(...);
    }

    // other code as before ...
}

现在在你的主应用程序中:

public class MainApp  extends Application {

    private Stage1Controller stage1Controller ;
    private Stage2Controller stage2Controller ;

    @Override
    public void start(Stage primaryStage) {
        firstStage();
        secondStage();

        stage1Controller.setStage2Controller(stage2Controller);

        // ...
    }

    public void firstStage() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/stage1.fxml"));
            rootLayout = (BorderPane) loader.load();

            stage1Controller = loader.getController();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void secondStage() {

        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/stage2.fxml"));
            XD = (AnchorPane) loader.load();

            stage2Controller = loader.getController();

            // Show the scene containing the root layout.
            Scene scene = new Scene(XD);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // ...
}
但是,更强大的方法是让两个控制器访问共享模型类,存储数据。如果使用JavaFX可观察属性表示数据,则控制器可以侦听对其关注的属性的更改。例如:

public class Model {

    private final StringProperty text = new SimpleStringProperty("Initial text...");

    public StringProperty textProperty() {
        return text ;
    }

    public final void setText(String text) {
        textProperty().set(text);
    }

    public final String getText() {
        return textProperty().get();
    }

    // other properties as needed...
}

现在您的控制器将如下所示:

public class Stage1Controller {

    private Model model ;

    public void setModel(Model model) {
        this.model = model ;
    }

    @FXML 
    private void handleTranslate (ActionEvent event){
        translateFirstStage();

        model.setText(...); // value will appear in stage2 controller's label!
    }

    // ...
}

public class Stage2Controller {

    @FXML
    private Label lb ;

    private Model model ;

    public void setModel(Model model) {
        lb.textProperty().unbind();
        this.model = model ;
        lb.textProperty().bind(model.textProperty());
    }

    // ...
}

在这种情况下,您的主应用程序如下:

public class MainApp extends Application {

    private final Model = new Model();

    @Override
    public void start(Stage primaryStage) {
        // ...
    }

    public void firstStage() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/stage1.fxml"));
            rootLayout = (BorderPane) loader.load();

            Stage1Controller controller = loader.getController();
            controller.setModel(model);

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void secondStage() {

        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/stage2.fxml"));
            XD = (AnchorPane) loader.load();

            Stage2Controller controller = loader.getController();
            controller.setModel(model);

            // Show the scene containing the root layout.
            Scene scene = new Scene(XD);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}