4

I'd like to ask if it is possible to pass a Variable through a JavaFX Class what extends Application to my JavaFx Controller? I am very new to JavaFx and only may need a little kick.

The goal is to pass a Id from MyClass to MyController.

My Application class:

public class MyClass extends Application {
    private String myVariable="Anything";

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

    @Override
    public void start(Stage stage) throws Exception {
        URL location = getClass().getResource("MyGui.fxml");

        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        Parent root = FXMLLoader.load(location);
        Scene scene = new Scene(root);

        stage.setTitle(title);
        stage.setScene(scene);
        stage.show();
    }
}

My Controller:

public class Controller extends Group implements Binding {

public void initialize(Map<String, Object> namespace, URL location, Resources resources) {

// HERE I'D LIKE TO GET MY VARIABLE LIKE
System.out.println(myVariable);
}

@Override
public List<Handler> getHandlerChain() {
    return null;
}

@Override
public void setHandlerChain(List<Handler> chain) {
}

@Override
public String getBindingID() {
    return null;
}
}
Rob
  • 13,342
  • 26
  • 40
  • 60
Artur Rem
  • 209
  • 1
  • 4
  • 15
  • It seems like its compareable to this: http://stackoverflow.com/questions/10751271/accessing-fxml-controller-class – Artur Rem Jun 25 '14 at 13:55
  • There is no need to use static variables as proposed by the accepted answer. See detailed answers to [this](https://stackoverflow.com/questions/10751271/accessing-fxml-controller-class) post and very detailed answer to [this](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) post – c0der Oct 26 '18 at 05:50

1 Answers1

3

First you will have to add the setter and getter in the MyClass (as the var is private) and change it to static:

private static String myVariable;
public String getMyVariable() {
    return myVariable;
}

public void setMyVariable(String myVariable) {
    this.myVariable = myVariable;
}

Then, as the MyClass is static can do:

System.out.println(MyClass.getMyVariable());

Working example:

MyClass.java

import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MyClass extends Application {
    private static String myVariable;

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

    @Override
    public void start(Stage stage) throws Exception {
        setMyVariable("Anything");
        URL location = getClass().getResource("MyGui.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        Parent root = FXMLLoader.load(location);
        Scene scene = new Scene(root);
        stage.setTitle("Hello Word");
        stage.setScene(scene);
        stage.show();
    }

    public static String getMyVariable() {
        return myVariable;
    }

    public static void setMyVariable(String myVariable) {
        MyClass.myVariable = myVariable;
    }

}

MyController.java

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;


public class MyController implements Initializable{
    @FXML Label labelVar;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        labelVar.setText(labelVar.getText() + MyClass.getMyVariable());

    }

}

MyGui.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="110.0" prefWidth="305.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
    <children>
        <Label fx:id="labelVar" layoutX="24.0" layoutY="32.0" prefHeight="17.0" prefWidth="129.0" text="MyVariable = " />
    </children>
</Pane>
Mansueli
  • 4,463
  • 6
  • 27
  • 50
  • 1
    This will not work because the getters and setters are not static, so referencing the methods from the class name is not possible. – CAG Gonzo Jun 25 '14 at 18:57
  • 1
    @CAGGonzo, it will as long as you make them `static`. Thanks for pointing out that I missed it in the original answer. – Mansueli Jun 25 '14 at 19:21
  • 1
    Thank you very much, i did by myself but in the same way. – Artur Rem Jun 26 '14 at 10:03
  • There is no need to use static variables. See detailed answers to [this](https://stackoverflow.com/questions/10751271/accessing-fxml-controller-class) post and very detailed answer to [this](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) post – c0der Oct 26 '18 at 05:51