JavaFX多个界面中的数据传递

JavaFX多个界面中的数据传递

经常遇到多个界面的数据传递问题,其实这个问题的解决方法很多,但是刚学JavaFX的可能不太熟悉,我在想这里简单介绍几种方法。

getController()
JavaFX多个界面中的数据传递_第1张图片

public class SourceDataController {
     

    public TextField name;
    public TextField age;

    public void submit(ActionEvent event) throws IOException {
     
        Stage stage = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/TargetData.fxml"));
        Parent parent = loader.load();
        TargetDataController target = loader.getController();
        target.name.setText(name.getText());
        target.age.setText(age.getText());
        Scene scene = new Scene(parent);
        stage.setScene(scene);
        stage.show();
    }
}

点击submit按钮之后,新建stage,获取controller设置传递值。
JavaFX多个界面中的数据传递_第2张图片
setUserData()
submit代码使用setUserData()设置,另一个界面使用getUserData()获取。

public void submit(ActionEvent event) throws IOException {
     
        Stage stage = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/TargetData.fxml"));
        Parent parent = loader.load();
//        TargetDataController target = loader.getController();
//        target.name.setText(name.getText());
//        target.age.setText(age.getText());
        Scene scene = new Scene(parent);
        Map<String, String> map = new HashMap<>();
        map.put("name",name.getText());
        map.put("age",age.getText());
        scene.setUserData(map);
        stage.setScene(scene);
        stage.show();
    }
public class TargetDataController {
     
    public Label name;
    public Label age;


    public void getData(ActionEvent event) {
     
        Scene scene = name.getScene();
        Map<String, String> userData = (Map<String, String>) scene.getUserData();
        String name = userData.get("name");
        String age = userData.get("age");
        this.name.setText(name);
        this.age.setText(age);
    }
}

这种方法比较适合不需要在另一个页面展示的数据,只是后台使用,而且在initialize方法中暂时获取不到,因为初始化阶段stage还没有显示,scene还是null。

JavaFX多个界面中的数据传递_第3张图片
使用单例viewmodel
使用mvvm模式,并将viewmodel设置为单例的,适合全局引用。
首先把示例改造为mvvm模式:
sourceController,submit调用viewmodel的setData()

public class SourceDataController {
     

    public TextField name;
    public TextField age;

    private SourceDataViewModel viewModel = SourceDataViewModel.getInstance();

    public void initialize(){
     
        viewModel.nameProperty().bindBidirectional(name.textProperty());
        viewModel.ageProperty().bindBidirectional(age.textProperty());
    }

    public void submit(ActionEvent event) throws IOException {
     
        viewModel.setTargetData();
        Stage stage = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/TargetData.fxml"));
        Parent parent = loader.load();
        Scene scene = new Scene(parent);
        stage.setScene(scene);
        stage.show();
    }
}

SourceDataViewModel,setData为另一个页面的viewModel设值

public class SourceDataViewModel {
     
    private StringProperty name = new SimpleStringProperty();
    private StringProperty age = new SimpleStringProperty();

    private static SourceDataViewModel viewModel = new SourceDataViewModel();

    private SourceDataViewModel(){
     }

    public static SourceDataViewModel getInstance() {
     
        return viewModel;
    }
    public void setTargetData() {
     
        TargetDataViewModel viewModel = TargetDataViewModel.getInstance();
        viewModel.setName(name.get());
        viewModel.setAge(age.get());

    }
	// getter and setter
}

TargetDataController

public class TargetDataController {
     
    public Label name;
    public Label age;

    private TargetDataViewModel viewModel = TargetDataViewModel.getInstance();

    public void initialize(){
     
        name.textProperty().bindBidirectional(viewModel.nameProperty());
        age.textProperty().bindBidirectional(viewModel.ageProperty());
    }
}

TargetDataViewModel

public class TargetDataViewModel {
     
    private StringProperty name = new SimpleStringProperty();
    private StringProperty age = new SimpleStringProperty();

    private static TargetDataViewModel viewModel = new TargetDataViewModel();

    private TargetDataViewModel(){
     }

    public static TargetDataViewModel getInstance(){
     
        return viewModel;
    }
    // getter and setter
}

JavaFX多个界面中的数据传递_第4张图片
简单的、复杂的方法都可以实现,看自己需要哪个。

你可能感兴趣的:(javafx,javafx,java)