Skip to content
Advertisement

Calling a method through from another controller JavaFx

RootLayoutControl.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);
    }
}

MainApp.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;
}

Creating an image gallery, where if I call the method,

handleOpen() in RootLayoutController

It’ll call method,

createImageView() in ImageViewController

To pass the variable file to it, any suggestions on how do I do it?

Advertisement

Answer

First, the createImageView() method needs to be public.

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

In RootLayoutController you need to create a method to get the instance of ImageViewController

private ImageViewController imageView;

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

Then you will need to get the controller and call the setImageView() method from RootLayoutController to pass its instance

FXMLLoader loader = new FXMLLoader(getClass().getResource("RootLayoutController.fxml"));
RootLayoutController controller = loader.getController();
controller.setImageView(imageView);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement