Display ProgressBar while dynamically loading GridPane
Heyyo,
So I'm currently working on a JavaFX project, with another Java project handling/serving the data for said project.
While I got most things working, I am still trying to get a ProgressBar to load while loading the data from the data project, which I am having some issues doing.
Currently, I'm looping through the data, creating/adding labels to a GridPane for each "entry". Above it, I declare a JavaFX ProgressBar and add it to my "root wrapper" VBox
.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : handler.get(from, to).entrySet())
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
wrapper.add(new Label(item.getKey()), 0, currentRow);
wrapper.add(new Label(inner.getDate().toString()), 1, currentRow);
wrapper.add(new Label(String.valueOf(inner.getValue())), 2, currentRow);
currentRow++;
viewBox.getChildren().add(wrapper);
Now, to the things I have tried to achieve this, as I realise this code alone won't do much:
1) Tried wrapping the GridPane in a Task
and creating a new thread from said task - Didn't work due to trying to create JavaFX elements while not being in the JavaFX Application Thread
2) Tried wrapping all of the wrapper.add(new Label(...));
in Platform.runLater()
- As wrapper
is not final and needs to exist both inside and outside of the scope, this did not work either.
3) Tried adding the data entry-by-entry to a temporary map and showing the progress that way by letting the ProgressBar-part run in it's own Task/Thread. Seemed to work as long as I used Thread.join();
afterwards, but seems extremely ineffective.
As always, any and all help is appreciated. If I forgot to mention something required to be able to tell what's going on, please do let me know! I've tried to include everything relevant.
Thanks in advance!
java multithreading javafx
add a comment |
Heyyo,
So I'm currently working on a JavaFX project, with another Java project handling/serving the data for said project.
While I got most things working, I am still trying to get a ProgressBar to load while loading the data from the data project, which I am having some issues doing.
Currently, I'm looping through the data, creating/adding labels to a GridPane for each "entry". Above it, I declare a JavaFX ProgressBar and add it to my "root wrapper" VBox
.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : handler.get(from, to).entrySet())
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
wrapper.add(new Label(item.getKey()), 0, currentRow);
wrapper.add(new Label(inner.getDate().toString()), 1, currentRow);
wrapper.add(new Label(String.valueOf(inner.getValue())), 2, currentRow);
currentRow++;
viewBox.getChildren().add(wrapper);
Now, to the things I have tried to achieve this, as I realise this code alone won't do much:
1) Tried wrapping the GridPane in a Task
and creating a new thread from said task - Didn't work due to trying to create JavaFX elements while not being in the JavaFX Application Thread
2) Tried wrapping all of the wrapper.add(new Label(...));
in Platform.runLater()
- As wrapper
is not final and needs to exist both inside and outside of the scope, this did not work either.
3) Tried adding the data entry-by-entry to a temporary map and showing the progress that way by letting the ProgressBar-part run in it's own Task/Thread. Seemed to work as long as I used Thread.join();
afterwards, but seems extremely ineffective.
As always, any and all help is appreciated. If I forgot to mention something required to be able to tell what's going on, please do let me know! I've tried to include everything relevant.
Thanks in advance!
java multithreading javafx
If I understand it correctly, you want to update a progressbar every time a "data" has been loaded? You should also show whether the code above is on the application thread or not.
– Andreas
Nov 14 '18 at 13:06
I want a progressbar that loads and "finishes" when the loop on the 6th row is finished, is a way to put it I guess. The code snippet is all currently in the application thread. @Andreas
– Xariez
Nov 14 '18 at 13:34
Regarding "1)", there are only a few JavaFX GUI objects that must be created on the FX thread; most of the objects are capable of being created and modified (so long as they aren't part of a scene-graph) on any single thread.
– Slaw
Nov 14 '18 at 13:35
add a comment |
Heyyo,
So I'm currently working on a JavaFX project, with another Java project handling/serving the data for said project.
While I got most things working, I am still trying to get a ProgressBar to load while loading the data from the data project, which I am having some issues doing.
Currently, I'm looping through the data, creating/adding labels to a GridPane for each "entry". Above it, I declare a JavaFX ProgressBar and add it to my "root wrapper" VBox
.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : handler.get(from, to).entrySet())
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
wrapper.add(new Label(item.getKey()), 0, currentRow);
wrapper.add(new Label(inner.getDate().toString()), 1, currentRow);
wrapper.add(new Label(String.valueOf(inner.getValue())), 2, currentRow);
currentRow++;
viewBox.getChildren().add(wrapper);
Now, to the things I have tried to achieve this, as I realise this code alone won't do much:
1) Tried wrapping the GridPane in a Task
and creating a new thread from said task - Didn't work due to trying to create JavaFX elements while not being in the JavaFX Application Thread
2) Tried wrapping all of the wrapper.add(new Label(...));
in Platform.runLater()
- As wrapper
is not final and needs to exist both inside and outside of the scope, this did not work either.
3) Tried adding the data entry-by-entry to a temporary map and showing the progress that way by letting the ProgressBar-part run in it's own Task/Thread. Seemed to work as long as I used Thread.join();
afterwards, but seems extremely ineffective.
As always, any and all help is appreciated. If I forgot to mention something required to be able to tell what's going on, please do let me know! I've tried to include everything relevant.
Thanks in advance!
java multithreading javafx
Heyyo,
So I'm currently working on a JavaFX project, with another Java project handling/serving the data for said project.
While I got most things working, I am still trying to get a ProgressBar to load while loading the data from the data project, which I am having some issues doing.
Currently, I'm looping through the data, creating/adding labels to a GridPane for each "entry". Above it, I declare a JavaFX ProgressBar and add it to my "root wrapper" VBox
.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : handler.get(from, to).entrySet())
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
wrapper.add(new Label(item.getKey()), 0, currentRow);
wrapper.add(new Label(inner.getDate().toString()), 1, currentRow);
wrapper.add(new Label(String.valueOf(inner.getValue())), 2, currentRow);
currentRow++;
viewBox.getChildren().add(wrapper);
Now, to the things I have tried to achieve this, as I realise this code alone won't do much:
1) Tried wrapping the GridPane in a Task
and creating a new thread from said task - Didn't work due to trying to create JavaFX elements while not being in the JavaFX Application Thread
2) Tried wrapping all of the wrapper.add(new Label(...));
in Platform.runLater()
- As wrapper
is not final and needs to exist both inside and outside of the scope, this did not work either.
3) Tried adding the data entry-by-entry to a temporary map and showing the progress that way by letting the ProgressBar-part run in it's own Task/Thread. Seemed to work as long as I used Thread.join();
afterwards, but seems extremely ineffective.
As always, any and all help is appreciated. If I forgot to mention something required to be able to tell what's going on, please do let me know! I've tried to include everything relevant.
Thanks in advance!
java multithreading javafx
java multithreading javafx
asked Nov 14 '18 at 12:53
XariezXariez
599518
599518
If I understand it correctly, you want to update a progressbar every time a "data" has been loaded? You should also show whether the code above is on the application thread or not.
– Andreas
Nov 14 '18 at 13:06
I want a progressbar that loads and "finishes" when the loop on the 6th row is finished, is a way to put it I guess. The code snippet is all currently in the application thread. @Andreas
– Xariez
Nov 14 '18 at 13:34
Regarding "1)", there are only a few JavaFX GUI objects that must be created on the FX thread; most of the objects are capable of being created and modified (so long as they aren't part of a scene-graph) on any single thread.
– Slaw
Nov 14 '18 at 13:35
add a comment |
If I understand it correctly, you want to update a progressbar every time a "data" has been loaded? You should also show whether the code above is on the application thread or not.
– Andreas
Nov 14 '18 at 13:06
I want a progressbar that loads and "finishes" when the loop on the 6th row is finished, is a way to put it I guess. The code snippet is all currently in the application thread. @Andreas
– Xariez
Nov 14 '18 at 13:34
Regarding "1)", there are only a few JavaFX GUI objects that must be created on the FX thread; most of the objects are capable of being created and modified (so long as they aren't part of a scene-graph) on any single thread.
– Slaw
Nov 14 '18 at 13:35
If I understand it correctly, you want to update a progressbar every time a "data" has been loaded? You should also show whether the code above is on the application thread or not.
– Andreas
Nov 14 '18 at 13:06
If I understand it correctly, you want to update a progressbar every time a "data" has been loaded? You should also show whether the code above is on the application thread or not.
– Andreas
Nov 14 '18 at 13:06
I want a progressbar that loads and "finishes" when the loop on the 6th row is finished, is a way to put it I guess. The code snippet is all currently in the application thread. @Andreas
– Xariez
Nov 14 '18 at 13:34
I want a progressbar that loads and "finishes" when the loop on the 6th row is finished, is a way to put it I guess. The code snippet is all currently in the application thread. @Andreas
– Xariez
Nov 14 '18 at 13:34
Regarding "1)", there are only a few JavaFX GUI objects that must be created on the FX thread; most of the objects are capable of being created and modified (so long as they aren't part of a scene-graph) on any single thread.
– Slaw
Nov 14 '18 at 13:35
Regarding "1)", there are only a few JavaFX GUI objects that must be created on the FX thread; most of the objects are capable of being created and modified (so long as they aren't part of a scene-graph) on any single thread.
– Slaw
Nov 14 '18 at 13:35
add a comment |
2 Answers
2
active
oldest
votes
After you've created your progressbar and it shows up on your GUI use
progress.setProgress(double value)
with an appropriate value depending on how far you've come in your progress.
As an example, you could put the progress like following:
progress.setProgress((currentRow - 1) / handler.get(from, to).entrySet().size())
This line can be put as first line in your for loop. When the whole job is done set the progress to 1 or remove the progress bar.
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
setProgress
should not be called from a background thread.
– fabian
Nov 14 '18 at 20:51
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
add a comment |
Nodes can be created in any thread, but you must not add them to a scene from a background thread. You shouldn't update properties of nodes in a scene from a background thread either.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
viewBox.getChildren().add(wrapper);
new Thread(() ->
Collection<Map.Entry<String, List<Observation>>> entries = handler.get(from, to).entrySet();
final double totalEntries = entries.size();
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : entries)
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
final Node nds = new Node
new Label(item.getKey()),
new Label(inner.getDate().toString()),
new Label(new Label(String.valueOf(inner.getValue())))
;
final double newProgress = currentRow / totalEntries;
final int r = currentRow;
Platform.runLater(() ->
wrapper.addRow(r, nds);
progress.setProgress(newProgress);
);
currentRow++;
).start();
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%2f53300726%2fdisplay-progressbar-while-dynamically-loading-gridpane%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
After you've created your progressbar and it shows up on your GUI use
progress.setProgress(double value)
with an appropriate value depending on how far you've come in your progress.
As an example, you could put the progress like following:
progress.setProgress((currentRow - 1) / handler.get(from, to).entrySet().size())
This line can be put as first line in your for loop. When the whole job is done set the progress to 1 or remove the progress bar.
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
setProgress
should not be called from a background thread.
– fabian
Nov 14 '18 at 20:51
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
add a comment |
After you've created your progressbar and it shows up on your GUI use
progress.setProgress(double value)
with an appropriate value depending on how far you've come in your progress.
As an example, you could put the progress like following:
progress.setProgress((currentRow - 1) / handler.get(from, to).entrySet().size())
This line can be put as first line in your for loop. When the whole job is done set the progress to 1 or remove the progress bar.
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
setProgress
should not be called from a background thread.
– fabian
Nov 14 '18 at 20:51
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
add a comment |
After you've created your progressbar and it shows up on your GUI use
progress.setProgress(double value)
with an appropriate value depending on how far you've come in your progress.
As an example, you could put the progress like following:
progress.setProgress((currentRow - 1) / handler.get(from, to).entrySet().size())
This line can be put as first line in your for loop. When the whole job is done set the progress to 1 or remove the progress bar.
After you've created your progressbar and it shows up on your GUI use
progress.setProgress(double value)
with an appropriate value depending on how far you've come in your progress.
As an example, you could put the progress like following:
progress.setProgress((currentRow - 1) / handler.get(from, to).entrySet().size())
This line can be put as first line in your for loop. When the whole job is done set the progress to 1 or remove the progress bar.
answered Nov 14 '18 at 14:05
AndreasAndreas
898
898
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
setProgress
should not be called from a background thread.
– fabian
Nov 14 '18 at 20:51
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
add a comment |
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
setProgress
should not be called from a background thread.
– fabian
Nov 14 '18 at 20:51
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
Seems to be working, however also seem to be having some other unexpected errors. Still, answers the question so i'll mark it as the answer! Thanks!
– Xariez
Nov 14 '18 at 14:57
setProgress
should not be called from a background thread.– fabian
Nov 14 '18 at 20:51
setProgress
should not be called from a background thread.– fabian
Nov 14 '18 at 20:51
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
He commented that it was in the application thread
– Andreas
Nov 15 '18 at 20:38
add a comment |
Nodes can be created in any thread, but you must not add them to a scene from a background thread. You shouldn't update properties of nodes in a scene from a background thread either.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
viewBox.getChildren().add(wrapper);
new Thread(() ->
Collection<Map.Entry<String, List<Observation>>> entries = handler.get(from, to).entrySet();
final double totalEntries = entries.size();
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : entries)
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
final Node nds = new Node
new Label(item.getKey()),
new Label(inner.getDate().toString()),
new Label(new Label(String.valueOf(inner.getValue())))
;
final double newProgress = currentRow / totalEntries;
final int r = currentRow;
Platform.runLater(() ->
wrapper.addRow(r, nds);
progress.setProgress(newProgress);
);
currentRow++;
).start();
add a comment |
Nodes can be created in any thread, but you must not add them to a scene from a background thread. You shouldn't update properties of nodes in a scene from a background thread either.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
viewBox.getChildren().add(wrapper);
new Thread(() ->
Collection<Map.Entry<String, List<Observation>>> entries = handler.get(from, to).entrySet();
final double totalEntries = entries.size();
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : entries)
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
final Node nds = new Node
new Label(item.getKey()),
new Label(inner.getDate().toString()),
new Label(new Label(String.valueOf(inner.getValue())))
;
final double newProgress = currentRow / totalEntries;
final int r = currentRow;
Platform.runLater(() ->
wrapper.addRow(r, nds);
progress.setProgress(newProgress);
);
currentRow++;
).start();
add a comment |
Nodes can be created in any thread, but you must not add them to a scene from a background thread. You shouldn't update properties of nodes in a scene from a background thread either.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
viewBox.getChildren().add(wrapper);
new Thread(() ->
Collection<Map.Entry<String, List<Observation>>> entries = handler.get(from, to).entrySet();
final double totalEntries = entries.size();
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : entries)
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
final Node nds = new Node
new Label(item.getKey()),
new Label(inner.getDate().toString()),
new Label(new Label(String.valueOf(inner.getValue())))
;
final double newProgress = currentRow / totalEntries;
final int r = currentRow;
Platform.runLater(() ->
wrapper.addRow(r, nds);
progress.setProgress(newProgress);
);
currentRow++;
).start();
Nodes can be created in any thread, but you must not add them to a scene from a background thread. You shouldn't update properties of nodes in a scene from a background thread either.
GridPane wrapper = new GridPane();
ProgressBar progress = new ProgressBar();
viewBox.getChildren().add(progress);
viewBox.getChildren().add(wrapper);
new Thread(() ->
Collection<Map.Entry<String, List<Observation>>> entries = handler.get(from, to).entrySet();
final double totalEntries = entries.size();
int currentRow = 1;
for(Map.Entry<String, List<Observation>> item : entries)
if(item != null)
for(Observation inner : item.getValue())
if(inner != null)
// Add the data for the datalist
final Node nds = new Node
new Label(item.getKey()),
new Label(inner.getDate().toString()),
new Label(new Label(String.valueOf(inner.getValue())))
;
final double newProgress = currentRow / totalEntries;
final int r = currentRow;
Platform.runLater(() ->
wrapper.addRow(r, nds);
progress.setProgress(newProgress);
);
currentRow++;
).start();
answered Nov 14 '18 at 21:25
fabianfabian
52.1k115272
52.1k115272
add a comment |
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%2f53300726%2fdisplay-progressbar-while-dynamically-loading-gridpane%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
If I understand it correctly, you want to update a progressbar every time a "data" has been loaded? You should also show whether the code above is on the application thread or not.
– Andreas
Nov 14 '18 at 13:06
I want a progressbar that loads and "finishes" when the loop on the 6th row is finished, is a way to put it I guess. The code snippet is all currently in the application thread. @Andreas
– Xariez
Nov 14 '18 at 13:34
Regarding "1)", there are only a few JavaFX GUI objects that must be created on the FX thread; most of the objects are capable of being created and modified (so long as they aren't part of a scene-graph) on any single thread.
– Slaw
Nov 14 '18 at 13:35