trishtren 0 Newbie Poster

Hello,
Iv recently just started using javafx and i am struggling finding a way to access data from a controller class, at the moment i can do it using method.invoke, however it seems like more of a hack than a legitimate clean option of accessing data from a controller class.

try {
            URL location = getClass().getResource("newwindow.fxml");
            FXMLLoader fxmlloader = new FXMLLoader();
            fxmlloader.setLocation(location);
            fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());
            Parent root = (Parent) fxmlloader.load(location.openStream());
            //above defines fxml class from the fxml file

            Stage stage = new Stage();
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
            //above gets stage from fxml file



             Method m = fxmlloader.getController().getClass().getMethod("getrows");
             System.out.println(m.invoke(fxmlloader.getController()));
        }
        catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) 
        {
            Logger.getLogger(EngineEditorController.class.getName()).log(Level.SEVERE, null, ex);
        }    

the above is how i am accessing the controller class method getrows() at the moment, im looking for a cleaner way either via fxml somehow or a way to pass data to the parent controller, i.e new file window is called from my main window. The controller class code is like so :

public class newWindowController implements Initializable {


    @FXML private Button okbutton;
    @FXML private Button cancelbutton;

    private int rowreturn; //used to return the number of items from controller
    private int columnreturn;


    @FXML
    private void handleOkButtonAction(ActionEvent event) throws IOException {

        System.out.println("connected");
    }


    public int getrows()
    {

      return rowreturn;  
    }

If anyone knows if this is the correct way to access data from a controller class or knows a better way any help would be appreciated, it doesent seem to be well documented by oracle yet...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.