-1

I have this simple controller:

@FXML
private VBox VVbox;

private ButtonBar newNode = new ButtonBar();
private Circle c= new Circle();
private Button b= new Button();
private Label lname = new Label();
private Label lIMEI = new Label();
private Label lroot = new Label();


@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

public void create(String imei){
    System.out.println(imei);
    newNode = new ButtonBar();
    b = setButtonSpec(imei + "btnHavefun");
    c = setCircleSpec(imei + "statuOnline");
    lname= setLNameSpec(imei + "name");
    lIMEI = setLIMEISpec(imei + "Imei");
    lroot = setLrootSpec(imei + "root");
    newNode.getButtons().addAll(lname,lIMEI,lroot,b,c);
    VVbox.getChildren().addAll(newNode) ;
}

this is my main:

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Thypheon Application");
    Connection connessione = new Connection();
    Thread t = new Thread(connessione);
    initDesign();
    t.start();
}


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

public void initDesign(){
    try {
        loader2= new FXMLLoader(getClass().getResource("Design.fxml"));
        AnchorPane anchor  = (AnchorPane) loader2.load();
        rootLayout.setCenter(anchor);
        controller = loader2.getController();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

As you can see in main I start a new thread in which I would like a Controller function.

public class Connection implements Runnable {
String result;
Controller controller = new Controller();
public void run() {
    controller.create("TEST123");
}

Everything seems to be inside create function until The last line is executed: VVbox.getChildren().addAll(newNode) ; Probably because it has a reference to the FXML file.. How can I solve this?

Alessio Trecani
  • 713
  • 2
  • 10
  • 25
  • For the general concept of communicating with a controller see [Passing Parameters JavaFX FXML](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). In your specific case you create a new controller you create a new controller that has none of it's fields injected, since it's not used with `FXMLLoader`. – fabian Feb 11 '16 at 11:27
  • Oh i will try this.. I will check better that post – Alessio Trecani Feb 11 '16 at 11:30

1 Answers1

2

Yes you are right. The controller that you instantiate yourself does not get its fields injected by FXML. To obtain a reference to the controller the following is a possible solution:

public Controller initDesign(){
    // some FXML loading code
    return controller;
}

You will then need to modify your Connection to take a Controller object in the constructor:

class Connection ... {
   Controller contoller;
   public Connection(Controller controller) {
       this.controller = controller;
   }
}

Finally in start() you will need:

Controller controller = initDesign();
Connection connessione = new Connection(controller);
Thread t = new Thread(connessione);
t.start();

However, there is more than one issue with your design. Your Connection instance is NOT run on JavaFX Application Thread. Therefore, any attempt to modify the scene graph from a different thread (e.g. your call to VVbox.getChildren().addAll(newNode);) will cause an error.

The start() method is called from JavaFX Thread. There is no need to create a new thread. I am unsure of the intentions, but you can call your create() from Controller in start() to be executed on JavaFX Thread.

AlmasB
  • 3,377
  • 2
  • 15
  • 17
  • Thank you so much for the clarify.. i will check it and I will set your answer as best as soon as possible. Just a little question how can I implement A JAVAFX Thread.. is it different from normale thread? – Alessio Trecani Feb 11 '16 at 11:33
  • 1
    The start() method is called from JavaFX Thread. There is no need to create a new thread. I am unsure of the intentions, but you can call your create() from Controller in start() to be executed on JavaFX Thread. – AlmasB Feb 11 '16 at 11:35
  • Ok man I simply created the class inside the MainApp thread and everything worked.. So pratically I ve made what you adviced me in the comment above. If you update the answer with that.. I can check it as Solved. – Alessio Trecani Feb 11 '16 at 11:53