Reputation: 71
I was working with the label.But when I used the label.settext("Something"). It was working in the background that means it sets the string but it doesn't show it on the screen. You can ask me how you know this? I checked it on my console by using System.out.println(label.gettext()). It gives the correct output. But the level did not show "Something".
serialPort.addEventListener((SerialPortEvent serialPortEvent) -> {
try{
System.out.println("Your selected text for label : "+objectname);
objectname="Nothing";
label2.setText(objectname);
System.out.println("Label gettext : "+label2.getText());
}catch(Exception e){
e.printStackTrace();
}
}
And it gives -> java.lang.IllegalStateException: Not on FX application thread; currentThread = EventThread COM5
Upvotes: 0
Views: 238
Reputation: 71
Finally, This works for me: As I was invoking SerialPortEvent in a separate thread, so it required to use Platform.runLater(() -> { }); Right code is given below:
Platform.runLater(() -> {
if(objectname!= null){
System.out.println("Previousobject : "+previousobject);
label2.setText(previousobject);
}
});
Upvotes: 0
Reputation: 2210
Every change to any JavaFX Node
must be invoked in FX application thread. Provided exception explains this. I assume that SerialPortEvent
is invoked in separate thread, that is why you get an exception. To fix that set Label
text in Platform.runLater()
.
CODE:
serialPort.addEventListener((SerialPortEvent serialPortEvent) -> {
try {
System.out.println("Your selected text for label : " + objectname);
objectname = "Nothing";
Platform.runLater(() -> {
label2.setText(objectname);
});
System.out.println("Label gettext : " + label2.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1