0

A separate thread gets Controller through below line segment code

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("FXMLDocument.fxml"));
    try 
    {
        loader.load();
    } catch (Exception ex) 
    {
        System.out.println("Error loading");
    }
    FXMLDocumentController controller = loader.getController();

Controller contains a method dynamicUpdate(String data) , Now I am calling this method from the thread where I have got the controller as follow.

controller.dynamicUpdate("hello from the thread"+count);

now here is the method in the controller: FXMLDocumentController :

public  void dynamicUpdate(String data)
{
    helloLabel.setText(data);
}

Where helloLabel is the id of the label and in the controller it's an instance variable In xml it's inside pane:

  <Label fx:id="helloLabel" layoutX="182.0" layoutY="179.0" prefHeight="43.0" prefWidth="259.0" text="sdadsadasdsadasd" />

in Controller it's:

 @FXML
public Label helloLabel;

The issue is the method is being called and the console outputs are being print on the console but the text label is not being updated and neither any exception is being thrown. Any help, any reading will be very great ! Thank you

  • You're not displaying the FXML that contains the `ListView` (you don't even keep a reference to the result of calling `loader.load()`). So you're updating a `ListView` that isn't even displayed. – James_D Nov 07 '17 at 12:34
  • let me add more details – Jalal Hussain Barzi Nov 07 '17 at 12:44
  • Sure... please do. I meant `Label` instead of `ListView`, btw (I was confusing this with a very similar question posted a little earlier). – James_D Nov 07 '17 at 12:50
  • For instance if I call same 'helloLabel.setText("Hello World")' it gets updates on a button click but it isn't from another method in the same controller i have tried with task and binding with property and then updating, it still doesn't works. – Jalal Hussain Barzi Nov 07 '17 at 12:51
  • But you loaded the FXML (via `loader.load()`), which creates a new label (along with the rest of the UI defined in the FXML file), but *that label is never displayed*. So you don't see the results of changing its text. – James_D Nov 07 '17 at 12:52
  • but the xml contains the controller and the method is in that controller and the controller is again returned from the loader then it should be changed. – Jalal Hussain Barzi Nov 07 '17 at 12:56
  • Again, the controller does change the text of the label (the one that is created when you call `loader.load()`), but (again) that label isn't displayed anywhere. So what do you expect to see? – James_D Nov 07 '17 at 12:56
  • What's the right way then – Jalal Hussain Barzi Nov 07 '17 at 12:58
  • Hard to say without actually seeing enough code to know what you are doing. I assume you must be loading the FXML *and displaying the result* somewhere. You need a reference to the controller for the UI that is created when you load *and display* the FXML. You probably need to create and post a [MCVE]. – James_D Nov 07 '17 at 13:00
  • Sure let me do that – Jalal Hussain Barzi Nov 07 '17 at 13:05
  • Perhaps see https://stackoverflow.com/questions/34118025/javafx-pass-values-from-child-to-parent or https://stackoverflow.com/questions/29639881/javafx-how-to-use-a-method-in-a-controller-from-another-controller or more generally https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D Nov 07 '17 at 13:34
  • Thank you I got your point, actually I was loading the xml twice and the changes are not made in the visible one,. – Jalal Hussain Barzi Nov 07 '17 at 13:38
  • hello James_D, can you please add it as an answer so that I should accept it as answer it has solved my problem. 'But you loaded the FXML (via loader.load()), which creates a new label (along with the rest of the UI defined in the FXML file), but that label is never displayed. So you don't see the results of changing its text. – Jalal Hussain Barzi Mar 09 '18 at 12:54

0 Answers0