Reputation: 65
I'm programing in JavaFX under java1.7, and everything works with my code except this part. The problem is, only the end result gets written out. While the program runs, I want it to display the "Ping test is running" text in the Label. But it won't do that, but instead waits a couple of seconds (till the ping finishes), than it writes out the result of the ping. Which looks bad, cause for 3-4 secs nothing happens and the user can think that it froze or something... hence the message I want to write out.
So, can someone tell me why this is happening and how to fix it? Thank you for your time.
PS: yes it needs to wait a few secs, in the PingIp(...)
, so i can read the needed info correctly, otherwise I'm getting the null pointer error.
@FXML
private Label Ping;
@FXML
private void Button(ActionEvent event) throws IOException {
String text;
Ping.setWrapText(true);
Ping.setText("Ping test is running");
text = PingIp.PingWifiAddr(GetIp.retIpWifi()).toString();
text = text.substring(1, (text.length()-1));
TeltonikaPing.setWrapText(true);
TeltonikaPing.setText(text);
}
below is the edited part
@FXML
private void ciscoButton(ActionEvent event) throws IOException {
CiscoPing.setWrapText(true);
Task<Void> task1 = new Task<Void>() {
@Override public Void call() throws IOException {
String text1;
text1 = "Ping teszt folyamatban";
Ping.setWrapText(true);
Ping.setText(text1);
return null;
}
};
new Thread(task1).start();
Task<Void> task = new Task<Void>() {
@Override public Void call() throws IOException {
String text2 = PingIp.PingEtherAddr(GetIp.retIpEther()).toString();
text2 = text2.substring(1, (text2.length() - 1));
Ping.setWrapText(true);
Ping.setText(text2);
return null;
}
};
new Thread(task).start();
}
Above is the edited one, any ideas?
Upvotes: 0
Views: 108
Reputation: 10613
The reason is because you're doing all of the work on the JavaFX Application Thread. When you set some attribute, like the label's text, on the JavaFX Application Thread the results don't immediately get displayed. Instead, it waits until your code is finished running, at which point the framework takes over again and can display your changes.
In order to get around this, you need to execute long running code on a separate thread, to avoid blocking the redrawing of the GUI. Take a look at Concurrency in JavaFX for details on how to implement that.
Upvotes: 1