JavaFX : Use an Indeterminate Progressbar in a splashScreen
I have a splash screen :
I need to have the animation of the progress bar (Indeterminate) but it doesn't work.
It's maybe due to because my thread is running in my initilize methode.
public class splashscreenController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
new SplashScreen().run();
}
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run()
Parent root = null;
try {
Thread.sleep(3000);
root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
assert root != null;
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
}
java javafx javafx-8 splash-screen
add a comment |
I have a splash screen :
I need to have the animation of the progress bar (Indeterminate) but it doesn't work.
It's maybe due to because my thread is running in my initilize methode.
public class splashscreenController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
new SplashScreen().run();
}
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run()
Parent root = null;
try {
Thread.sleep(3000);
root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
assert root != null;
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
}
java javafx javafx-8 splash-screen
Example of a JavaFX splash screen with progress bar monitoring Task progress. Note, critically, that theTask
is run in its own thread:new Thread(friendTask).start()
. To make it indefinite, just don't bind the progress bar's progress property to the task's progress property.
– jewelsea
Nov 14 '18 at 0:44
add a comment |
I have a splash screen :
I need to have the animation of the progress bar (Indeterminate) but it doesn't work.
It's maybe due to because my thread is running in my initilize methode.
public class splashscreenController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
new SplashScreen().run();
}
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run()
Parent root = null;
try {
Thread.sleep(3000);
root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
assert root != null;
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
}
java javafx javafx-8 splash-screen
I have a splash screen :
I need to have the animation of the progress bar (Indeterminate) but it doesn't work.
It's maybe due to because my thread is running in my initilize methode.
public class splashscreenController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
new SplashScreen().run();
}
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run()
Parent root = null;
try {
Thread.sleep(3000);
root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
assert root != null;
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
}
java javafx javafx-8 splash-screen
java javafx javafx-8 splash-screen
asked Nov 13 '18 at 16:49
charles Lgncharles Lgn
143217
143217
Example of a JavaFX splash screen with progress bar monitoring Task progress. Note, critically, that theTask
is run in its own thread:new Thread(friendTask).start()
. To make it indefinite, just don't bind the progress bar's progress property to the task's progress property.
– jewelsea
Nov 14 '18 at 0:44
add a comment |
Example of a JavaFX splash screen with progress bar monitoring Task progress. Note, critically, that theTask
is run in its own thread:new Thread(friendTask).start()
. To make it indefinite, just don't bind the progress bar's progress property to the task's progress property.
– jewelsea
Nov 14 '18 at 0:44
Example of a JavaFX splash screen with progress bar monitoring Task progress. Note, critically, that the
Task
is run in its own thread: new Thread(friendTask).start()
. To make it indefinite, just don't bind the progress bar's progress property to the task's progress property.– jewelsea
Nov 14 '18 at 0:44
Example of a JavaFX splash screen with progress bar monitoring Task progress. Note, critically, that the
Task
is run in its own thread: new Thread(friendTask).start()
. To make it indefinite, just don't bind the progress bar's progress property to the task's progress property.– jewelsea
Nov 14 '18 at 0:44
add a comment |
1 Answer
1
active
oldest
votes
There are 2 issues in your code:
new SplashScreen().run();
A Task
does not provide functionality for running on a new thread. run
is executed on the calling thread.
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// placeholder for parts of your code
longRunningOperation();
guiUpdate();
}
});
return null;
}
}
Even if you execute this task on a seperate thread, the Runnable
passed to Platfrom.runLater
is executed on the JavaFX application thread and doing long-running operations from this runnable freezes the GUI.
Do all the long-running operations on the background thread instead and only do short updates using Platfrom.runLater
.
new Thread(new SplashScreen()).start();
class SplashScreen extends Task {
@Override
public Object call() throws IOException, InterruptedException {
Thread.sleep(3000);
final Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
Note that since you're not using the functionality provided by Task
, you could simply implement Runnable
with your class instead of inheriting from Task
.
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
1
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).Platform.runLater
results in code being run on the javafx application thread, soThread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after usingPlatform.runLater
.
– fabian
Nov 13 '18 at 17:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285848%2fjavafx-use-an-indeterminate-progressbar-in-a-splashscreen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are 2 issues in your code:
new SplashScreen().run();
A Task
does not provide functionality for running on a new thread. run
is executed on the calling thread.
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// placeholder for parts of your code
longRunningOperation();
guiUpdate();
}
});
return null;
}
}
Even if you execute this task on a seperate thread, the Runnable
passed to Platfrom.runLater
is executed on the JavaFX application thread and doing long-running operations from this runnable freezes the GUI.
Do all the long-running operations on the background thread instead and only do short updates using Platfrom.runLater
.
new Thread(new SplashScreen()).start();
class SplashScreen extends Task {
@Override
public Object call() throws IOException, InterruptedException {
Thread.sleep(3000);
final Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
Note that since you're not using the functionality provided by Task
, you could simply implement Runnable
with your class instead of inheriting from Task
.
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
1
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).Platform.runLater
results in code being run on the javafx application thread, soThread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after usingPlatform.runLater
.
– fabian
Nov 13 '18 at 17:19
add a comment |
There are 2 issues in your code:
new SplashScreen().run();
A Task
does not provide functionality for running on a new thread. run
is executed on the calling thread.
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// placeholder for parts of your code
longRunningOperation();
guiUpdate();
}
});
return null;
}
}
Even if you execute this task on a seperate thread, the Runnable
passed to Platfrom.runLater
is executed on the JavaFX application thread and doing long-running operations from this runnable freezes the GUI.
Do all the long-running operations on the background thread instead and only do short updates using Platfrom.runLater
.
new Thread(new SplashScreen()).start();
class SplashScreen extends Task {
@Override
public Object call() throws IOException, InterruptedException {
Thread.sleep(3000);
final Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
Note that since you're not using the functionality provided by Task
, you could simply implement Runnable
with your class instead of inheriting from Task
.
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
1
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).Platform.runLater
results in code being run on the javafx application thread, soThread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after usingPlatform.runLater
.
– fabian
Nov 13 '18 at 17:19
add a comment |
There are 2 issues in your code:
new SplashScreen().run();
A Task
does not provide functionality for running on a new thread. run
is executed on the calling thread.
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// placeholder for parts of your code
longRunningOperation();
guiUpdate();
}
});
return null;
}
}
Even if you execute this task on a seperate thread, the Runnable
passed to Platfrom.runLater
is executed on the JavaFX application thread and doing long-running operations from this runnable freezes the GUI.
Do all the long-running operations on the background thread instead and only do short updates using Platfrom.runLater
.
new Thread(new SplashScreen()).start();
class SplashScreen extends Task {
@Override
public Object call() throws IOException, InterruptedException {
Thread.sleep(3000);
final Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
Note that since you're not using the functionality provided by Task
, you could simply implement Runnable
with your class instead of inheriting from Task
.
There are 2 issues in your code:
new SplashScreen().run();
A Task
does not provide functionality for running on a new thread. run
is executed on the calling thread.
class SplashScreen extends Task {
@Override
public Object call() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// placeholder for parts of your code
longRunningOperation();
guiUpdate();
}
});
return null;
}
}
Even if you execute this task on a seperate thread, the Runnable
passed to Platfrom.runLater
is executed on the JavaFX application thread and doing long-running operations from this runnable freezes the GUI.
Do all the long-running operations on the background thread instead and only do short updates using Platfrom.runLater
.
new Thread(new SplashScreen()).start();
class SplashScreen extends Task {
@Override
public Object call() throws IOException, InterruptedException {
Thread.sleep(3000);
final Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
Platform.runLater(new Runnable() {
@Override
public void run() {
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
Note that since you're not using the functionality provided by Task
, you could simply implement Runnable
with your class instead of inheriting from Task
.
answered Nov 13 '18 at 17:03
fabianfabian
51.2k115272
51.2k115272
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
1
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).Platform.runLater
results in code being run on the javafx application thread, soThread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after usingPlatform.runLater
.
– fabian
Nov 13 '18 at 17:19
add a comment |
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
1
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).Platform.runLater
results in code being run on the javafx application thread, soThread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after usingPlatform.runLater
.
– fabian
Nov 13 '18 at 17:19
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
So if I understand, my code is wrong because I need to stop my thread before the runLater. thank you now it works !
– charles Lgn
Nov 13 '18 at 17:13
1
1
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).
Platform.runLater
results in code being run on the javafx application thread, so Thread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after using Platform.runLater
.– fabian
Nov 13 '18 at 17:19
@charlesLgn "I need to stop the thread before" That's not really the case. Everything that matters is that operations on the javafx application thread complete fast (withing a few ms at most).
Platform.runLater
results in code being run on the javafx application thread, so Thread.sleep
is something you need to avoid doing in the runnable. The background thread can keep running for as long as you like after using Platform.runLater
.– fabian
Nov 13 '18 at 17:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285848%2fjavafx-use-an-indeterminate-progressbar-in-a-splashscreen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Example of a JavaFX splash screen with progress bar monitoring Task progress. Note, critically, that the
Task
is run in its own thread:new Thread(friendTask).start()
. To make it indefinite, just don't bind the progress bar's progress property to the task's progress property.– jewelsea
Nov 14 '18 at 0:44