-2

I need to pass a Stage to my Filechooser in the Controller Class.

For that I need to set a Controller in my MainDesignClass.

What is wrong here:

@Override
public void start( Stage primaryStage) throws Exception{
 FXMLLoader loader= new FXMLLoader(getClass().getResources("myfxml.fxml");
 Parent root =(Parent)loader.load();

primaryStage = new Stage();
Controller myController=loader.getController();
myController.setStage(primaryStage);
primaryStage.setTitle("myapp");
primaryStage.getIcons().add(image);
primaryStage.setScene(new Scene(root,900,600));
primaryStage.show();
    }

setStage is marked red. But why? Why it cannot find the method? How can I use FileChooser then in my Controller.class ?

Smart Geek
  • 37
  • 1
  • 4
  • What is your `Controller` class code? – D-Dᴙum Sep 18 '17 at 12:14
  • FileChooser chooser= new FileChooser(); File file= chooser.showOpenDialog(MainDesign.primaryStage); – Smart Geek Sep 18 '17 at 12:47
  • I meant can you please post the _whole_ of class Controller. I assume it's the line `myController.setStage(primaryStage);` giving the compile error? – D-Dᴙum Sep 18 '17 at 13:24
  • yes exactly. It is giving me an error because the Controller is set wrong in the main class and I dont kow how to set it. The controller doesnt have the right stage, eg. same stage as the main and I cannot set it. – Smart Geek Sep 18 '17 at 13:35
  • You need to post all your code. – D-Dᴙum Sep 18 '17 at 13:47
  • If you try this code, it already doesnt work. And this code shall work until where I posted it. That would be sufficient – Smart Geek Sep 18 '17 at 14:50

2 Answers2

0

To use Controller you must set it in your FXML file in a parent object like this <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="157.0" prefWidth="430.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">

Symph
  • 51
  • 8
  • this does not answer my question as I have already set it in the fxml file and I need to pass the stage to the Controller to be able to use it in my FileChooser, I want to pass the same stage and it does not work and there are some errors. – Smart Geek Sep 18 '17 at 12:46
  • If you want to get current Stage, then try Stage stage = (Stage) chooser.getScene().getWindow(); – Symph Sep 18 '17 at 12:51
  • this doesnt work as the Controller and the MainGUi are not in the same class – Smart Geek Sep 18 '17 at 13:05
  • Put `Stage stage = (Stage) chooser.getScene().getWindow();` in your controller in the method, where you need to do actions with you filechooser. It will take the Stage, where your _chooser_ or any other FXML element is present. – Symph Sep 18 '17 at 13:14
  • See this [first](https://stackoverflow.com/questions/40299170/how-to-reference-stage-in-controller-class)-[google](https://stackoverflow.com/questions/33932309/how-to-access-a-javafx-stage-from-a-controller)-[page](https://stackoverflow.com/questions/13015537/javafx-class-controller-stage-window-reference) answers – Symph Sep 19 '17 at 06:12
0

Ok, I got this solved:

Main Class

public class Main extends Application {

private static Stage primaryStage; // Declare static Stage, so it can also be accessed in the controller and if you walk through Files!

private void setPrimaryStage(Stage stage) {
    Main.primaryStage = stage;
}

static public Stage getPrimaryStage() {
    return Main.primaryStage;
}

@Override
public void start(Stage primaryStage) throws Exception{
    setPrimaryStage(primaryStage); // **Set the Stage**

//get new instance of FXMLLoader
        FXMLLoader loader= new FXMLLoader(getClass().getResources("myfxml.fxml");
        Parent root =(Parent)loader.load();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }
}

instanceofmain.getPrimaryStage()

In Controller Class

public class Controller {

private Main mymainclass; //you need an instance of the main class to open the stage on this very instance, as it is static you will be able to get it then.

public void onMouseClickAction(ActionEvent e) {
    Stage s = mymainclass.getPrimaryStage();
    // do not apply any close actions, as you want to stay on the same stage

FileChooser chooser= new Filechooser();
File defaultfile = chooser.showOpenDialog(s); // for directories the command is showDialog(s);
    }
    }

There were no other threads related to my specific problem.

Smart Geek
  • 37
  • 1
  • 4