java - 从另一个 Controller JavaFx调用方法

标签 java javafx imageview fxml

根布局控件.java

public class RootLayoutController {

private MainApp mainApp;


 public void setMainApp(MainApp mainApp) {
    // TODO Auto-generated method stub
    this.mainApp = mainApp;
}

@FXML
private void handleNew(){

}

@FXML
private void handleOpen(){
    FileChooser fileChooser = new FileChooser();

    //Set extension filter
    ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image Files", "*.png","*.jpg", "*.jpeg");

    fileChooser.getExtensionFilters().add(extFilter);

    // Show save file dialog
    File file = fileChooser.showOpenDialog(mainApp.getPrimaryStage());

    if(file != null){
        //CALL createImageView(file);
    }
}

主程序.java

public class MainApp extends Application {

 private Stage primaryStage;
 private BorderPane rootLayout;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Photo Album");

    initRootLayout();

    showImageView();
}

private void showImageView() {
    // TODO Auto-generated method stub
    try {
        // Load person overview.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("ImageView.fxml"));
        AnchorPane imageOverview = (AnchorPane) loader.load();

        ImageViewController controller = loader.getController();
        controller.setMainApp(this);

        // Set person overview into the center of root layout.
        rootLayout.setCenter(imageOverview);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public Stage getPrimaryStage() {
    return primaryStage;
}

public static void main(String[] args) {
    launch(args);
}

public void ShowView(File file) {
    // TODO Auto-generated method stub

}

ImageViewController.java

public class ImageViewController {

private MainApp mainApp;

@FXML
private ImageView imageView;

@FXML
private TilePane tile;

public void setMainApp(MainApp mainApp) {
    // TODO Auto-generated method stub
    this.mainApp = mainApp;
}

private ImageView createImageView(File file){
    return imageView;
}

创建图片库,如果我在其中调用该方法,

handleOpen() in RootLayoutController

它会调用方法,

createImageView() in ImageViewController

要将变量文件传递给它,我该怎么做有什么建议吗?

最佳答案

首先,createImageView() 方法需要公开。

public ImageView createImageView(File file){
    return imageView;
}

在 RootLayoutController 中你需要创建一个方法来获取 ImageViewController 的实例

private ImageViewController imageView;

public void setImageView(ImageViewController imageView) {
   this.imageView = imageView;
}

然后你需要获取 Controller 并从 RootLayoutController 调用 setImageView() 方法来传递它的实例

FXMLLoader loader = new FXMLLoader(getClass().getResource("RootLayoutController.fxml"));
RootLayoutController controller = loader.getController();
controller.setImageView(imageView);

关于java - 从另一个 Controller JavaFx调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33975747/

相关文章:

javafx datepicker 在 swin 面板上的 swing 中

javafx - TextArea 中的拼写检查文本

ios - Instagram 的缩放是如何工作的?

android - 这是纹理吗?

android - 我如何在 Android 中显示此图像?

java - 为什么Java中的两个嵌套类会导致异常?

java - Android Volley 无法返回字符串

java - 为什么它不将图像添加到流程 Pane ?

java - Eclipse Juno 插件项目中的 mysql jdbc 连接错误

java - 从自身中断线程