0

Related to my question : Bind label with two different values-javafx , now I am able to bind two values to a label and update on my UI in my eclipse. Since my application is updating the value very frequently I have done the bind work in a timer as shown :

Timeline timer = new Timeline(new KeyFrame(Duration.seconds(1), new 
EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
            answerQuesLbl.textProperty().bind(answerConnector.getNoOfAnswers().asString().concat("/").concat(answerConnector.getNoOfQuestions().asString()));
    }
}));

timer.setCycleCount(Timeline.INDEFINITE);
timer.play();

This is working fine within my eclise, but when I create a build of my project using
mvn assembly:assembly -Dmaven.test.skip=true

and running the project using bat file, everything works fine , even the values are getting updated but not on UI ,it is giving an exception like :

Exception in thread "Thread-7" java.lang.IllegalStateException: 
    Not on FX application thread; currentThread = Thread-7
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:229)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)

My build is good, do i need to create different kind of thread for this?

Chetna rustagi
  • 463
  • 7
  • 21
  • This seem to be a [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), since the binding doesn't worked as expected you tried to fix it with a timeline. Can you create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) so we can see what `answerConnector` exactly is and how it's changed. And then we could properly fix your binding-problem. –  Feb 20 '18 at 11:56
  • Why are you using `Timeline` to do this? Why it is important to bind in every second those values to the label's textProperty? Since bindings ensure the update of the bound property, you could simply bind it and it should work. – Sunflame Feb 20 '18 at 11:56
  • @Sunflame using `Timeline` since my value is getting updated 10 times in a second , I dont want to load the application soo much ,thatsy performing the update only once a second on the label – Chetna rustagi Feb 20 '18 at 12:07
  • 1
    Do you have a second thread which updates the values or how does it come the value change _10 times in a second_? Also the reiterated binding makes this only worse (In this case you could simply update the text directly, but don't do this) –  Feb 20 '18 at 12:13
  • 1
    Note: Depending on what Property you have, for example with `SimpleIntegerProperty` the UI is only changed if the value really changes and not if it is only set again. –  Feb 20 '18 at 12:20
  • Yes the values are getting updated by a different thread, and also yes the `SimpleIntegerProperty ` is really getting changed. I will change this logic and get back to you if I get any problem – Chetna rustagi Feb 20 '18 at 12:46
  • Even if the value is changing in every 0.1 second, you can let it change via bindings. If you really want to update it just in every second, you can ignore the binding and simply `.set` the value in that `Timeline` instead of bind it. – Sunflame Feb 20 '18 at 13:04
  • 1
    Also see https://stackoverflow.com/questions/23488280/throttling-javafx-gui-updates for ways to restrict (in an optimal way) the number of updates to the UI. – James_D Feb 20 '18 at 13:36

1 Answers1

2

There are two issues with your code:
1. There is no need to re-bind every second.
2. Bound properties receive change notifications on the thread that performed the change, so if answerConnector.getNoOfAnswers is changed on another thread, you will get a not-on-FX-thread exception.

You can either change the event handler so that you set the text every second (the Timeline ensures it will be called on the FX thread), or else make sure the original changed is performed on the FX thread.

Itai
  • 6,641
  • 6
  • 27
  • 51
  • Curious to know that why does it doesnt gives this exception `Not on FX Application Thread` while I am running this code in my eclipse. – Chetna rustagi Feb 21 '18 at 06:13
  • That would be nearly impossible to tell without a [mcve]. Could be down to environment/configuration, or could be something tricky like timing debugger messing with threads... – Itai Feb 21 '18 at 08:20