Changing items in GridPane Using a different Thread










0














I have a GridPane with an image on it. I would like the image to change its position (go to a different grid child node) every 2 seconds. However when I ran the program using another thread I get: Not on FX application thread error. Any help is appreciated!



I have tried using Platform.runLater as well but it still doesn't work. Also by reading through different posts I learned that GridPane is not thread safe. Is there a way to bypass that?



public class BoardController implements Initializable 
@FXML
private GridPane board;
@FXML
private Text position;

String pikachuPos = "";
String pokeballPos = "";
int i = 0;
int j = 0;
boolean selectPikachu = true;
int count = 0;

int pikachuRow = 0;
int pikachuCol = 0;
int pokeballRow = 0;
int pokeballCol = 0;

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

position.setVisible(true);
if (count == 2)
System.out.println("2");

boolean pikachu = true;

Button buttons = new Button[10][10];
Button b;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
b = new Button(j + " " + i);
buttons[i][j] = b;
buttons[i][j].setOnAction((event) ->
if (this.selectPikachu)
Button button = (Button) event.getSource();
this.pikachuPos = button.getText();
this.selectPikachu = false;
this.position.setText("Choose Pokeball position");
else
Button button = (Button) event.getSource();
this.pokeballPos = button.getText();
this.position.setText("");
try
this.play();
catch (InterruptedException ex)
Logger.getLogger(BoardController.class.getName()).log(Level.SEVERE, null, ex);



);

board.add(buttons[i][j], i, j);
GridPane.setHalignment(buttons[i][j], HPos.CENTER);




public void play() throws InterruptedException

Node node = board.getChildren().get(0);
board.getChildren().clear();
board.getChildren().add(0, node);

String pikachuDetails = pikachuPos.split(" ");
String pokeballDetails = pokeballPos.split(" ");

pikachuRow = Integer.parseInt(pikachuDetails[1]);
pikachuCol = Integer.parseInt(pikachuDetails[0]);

pokeballRow = Integer.parseInt(pokeballDetails[1]);
pokeballCol = Integer.parseInt(pokeballDetails[0]);

ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);

GridPane.setHalignment(pokeball, HPos.CENTER);
board.add(pokeball, pokeballRow, pokeballCol);

GridPane.setHalignment(pikachu, HPos.CENTER);
board.add(pikachu, pikachuRow, pikachuCol);

startTask();



public void startTask()
// Create a Runnable
Runnable task = new Runnable()
public void run()
runTask();

;

// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();


public void runTask()
for (int i = 1; i <= 10; i++)
try
board.add(pikachu, 5, 5);
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();




public void setPikachu(int r, int c)
board.add(pikachu, r, c);
GridPane.setHalignment(pikachu, HPos.CENTER);












share|improve this question























  • Possible duplicate of stackoverflow.com/questions/21083945/…
    – quant
    Nov 13 '18 at 0:25










  • Explain what "still doesn't work" means when you use Platform.runLater. Are you still getting exceptions?
    – Slaw
    Nov 13 '18 at 1:42















0














I have a GridPane with an image on it. I would like the image to change its position (go to a different grid child node) every 2 seconds. However when I ran the program using another thread I get: Not on FX application thread error. Any help is appreciated!



I have tried using Platform.runLater as well but it still doesn't work. Also by reading through different posts I learned that GridPane is not thread safe. Is there a way to bypass that?



public class BoardController implements Initializable 
@FXML
private GridPane board;
@FXML
private Text position;

String pikachuPos = "";
String pokeballPos = "";
int i = 0;
int j = 0;
boolean selectPikachu = true;
int count = 0;

int pikachuRow = 0;
int pikachuCol = 0;
int pokeballRow = 0;
int pokeballCol = 0;

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

position.setVisible(true);
if (count == 2)
System.out.println("2");

boolean pikachu = true;

Button buttons = new Button[10][10];
Button b;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
b = new Button(j + " " + i);
buttons[i][j] = b;
buttons[i][j].setOnAction((event) ->
if (this.selectPikachu)
Button button = (Button) event.getSource();
this.pikachuPos = button.getText();
this.selectPikachu = false;
this.position.setText("Choose Pokeball position");
else
Button button = (Button) event.getSource();
this.pokeballPos = button.getText();
this.position.setText("");
try
this.play();
catch (InterruptedException ex)
Logger.getLogger(BoardController.class.getName()).log(Level.SEVERE, null, ex);



);

board.add(buttons[i][j], i, j);
GridPane.setHalignment(buttons[i][j], HPos.CENTER);




public void play() throws InterruptedException

Node node = board.getChildren().get(0);
board.getChildren().clear();
board.getChildren().add(0, node);

String pikachuDetails = pikachuPos.split(" ");
String pokeballDetails = pokeballPos.split(" ");

pikachuRow = Integer.parseInt(pikachuDetails[1]);
pikachuCol = Integer.parseInt(pikachuDetails[0]);

pokeballRow = Integer.parseInt(pokeballDetails[1]);
pokeballCol = Integer.parseInt(pokeballDetails[0]);

ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);

GridPane.setHalignment(pokeball, HPos.CENTER);
board.add(pokeball, pokeballRow, pokeballCol);

GridPane.setHalignment(pikachu, HPos.CENTER);
board.add(pikachu, pikachuRow, pikachuCol);

startTask();



public void startTask()
// Create a Runnable
Runnable task = new Runnable()
public void run()
runTask();

;

// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();


public void runTask()
for (int i = 1; i <= 10; i++)
try
board.add(pikachu, 5, 5);
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();




public void setPikachu(int r, int c)
board.add(pikachu, r, c);
GridPane.setHalignment(pikachu, HPos.CENTER);












share|improve this question























  • Possible duplicate of stackoverflow.com/questions/21083945/…
    – quant
    Nov 13 '18 at 0:25










  • Explain what "still doesn't work" means when you use Platform.runLater. Are you still getting exceptions?
    – Slaw
    Nov 13 '18 at 1:42













0












0








0







I have a GridPane with an image on it. I would like the image to change its position (go to a different grid child node) every 2 seconds. However when I ran the program using another thread I get: Not on FX application thread error. Any help is appreciated!



I have tried using Platform.runLater as well but it still doesn't work. Also by reading through different posts I learned that GridPane is not thread safe. Is there a way to bypass that?



public class BoardController implements Initializable 
@FXML
private GridPane board;
@FXML
private Text position;

String pikachuPos = "";
String pokeballPos = "";
int i = 0;
int j = 0;
boolean selectPikachu = true;
int count = 0;

int pikachuRow = 0;
int pikachuCol = 0;
int pokeballRow = 0;
int pokeballCol = 0;

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

position.setVisible(true);
if (count == 2)
System.out.println("2");

boolean pikachu = true;

Button buttons = new Button[10][10];
Button b;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
b = new Button(j + " " + i);
buttons[i][j] = b;
buttons[i][j].setOnAction((event) ->
if (this.selectPikachu)
Button button = (Button) event.getSource();
this.pikachuPos = button.getText();
this.selectPikachu = false;
this.position.setText("Choose Pokeball position");
else
Button button = (Button) event.getSource();
this.pokeballPos = button.getText();
this.position.setText("");
try
this.play();
catch (InterruptedException ex)
Logger.getLogger(BoardController.class.getName()).log(Level.SEVERE, null, ex);



);

board.add(buttons[i][j], i, j);
GridPane.setHalignment(buttons[i][j], HPos.CENTER);




public void play() throws InterruptedException

Node node = board.getChildren().get(0);
board.getChildren().clear();
board.getChildren().add(0, node);

String pikachuDetails = pikachuPos.split(" ");
String pokeballDetails = pokeballPos.split(" ");

pikachuRow = Integer.parseInt(pikachuDetails[1]);
pikachuCol = Integer.parseInt(pikachuDetails[0]);

pokeballRow = Integer.parseInt(pokeballDetails[1]);
pokeballCol = Integer.parseInt(pokeballDetails[0]);

ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);

GridPane.setHalignment(pokeball, HPos.CENTER);
board.add(pokeball, pokeballRow, pokeballCol);

GridPane.setHalignment(pikachu, HPos.CENTER);
board.add(pikachu, pikachuRow, pikachuCol);

startTask();



public void startTask()
// Create a Runnable
Runnable task = new Runnable()
public void run()
runTask();

;

// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();


public void runTask()
for (int i = 1; i <= 10; i++)
try
board.add(pikachu, 5, 5);
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();




public void setPikachu(int r, int c)
board.add(pikachu, r, c);
GridPane.setHalignment(pikachu, HPos.CENTER);












share|improve this question















I have a GridPane with an image on it. I would like the image to change its position (go to a different grid child node) every 2 seconds. However when I ran the program using another thread I get: Not on FX application thread error. Any help is appreciated!



I have tried using Platform.runLater as well but it still doesn't work. Also by reading through different posts I learned that GridPane is not thread safe. Is there a way to bypass that?



public class BoardController implements Initializable 
@FXML
private GridPane board;
@FXML
private Text position;

String pikachuPos = "";
String pokeballPos = "";
int i = 0;
int j = 0;
boolean selectPikachu = true;
int count = 0;

int pikachuRow = 0;
int pikachuCol = 0;
int pokeballRow = 0;
int pokeballCol = 0;

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

position.setVisible(true);
if (count == 2)
System.out.println("2");

boolean pikachu = true;

Button buttons = new Button[10][10];
Button b;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
b = new Button(j + " " + i);
buttons[i][j] = b;
buttons[i][j].setOnAction((event) ->
if (this.selectPikachu)
Button button = (Button) event.getSource();
this.pikachuPos = button.getText();
this.selectPikachu = false;
this.position.setText("Choose Pokeball position");
else
Button button = (Button) event.getSource();
this.pokeballPos = button.getText();
this.position.setText("");
try
this.play();
catch (InterruptedException ex)
Logger.getLogger(BoardController.class.getName()).log(Level.SEVERE, null, ex);



);

board.add(buttons[i][j], i, j);
GridPane.setHalignment(buttons[i][j], HPos.CENTER);




public void play() throws InterruptedException

Node node = board.getChildren().get(0);
board.getChildren().clear();
board.getChildren().add(0, node);

String pikachuDetails = pikachuPos.split(" ");
String pokeballDetails = pokeballPos.split(" ");

pikachuRow = Integer.parseInt(pikachuDetails[1]);
pikachuCol = Integer.parseInt(pikachuDetails[0]);

pokeballRow = Integer.parseInt(pokeballDetails[1]);
pokeballCol = Integer.parseInt(pokeballDetails[0]);

ImageView pokeball = new ImageView(getClass().getResource("pokeball.png").toExternalForm());
pokeball.setFitWidth(30);
pokeball.setFitHeight(30);

ImageView pikachu = new ImageView(getClass().getResource("pikachu.png").toExternalForm());
pikachu.setFitWidth(30);
pikachu.setFitHeight(30);

GridPane.setHalignment(pokeball, HPos.CENTER);
board.add(pokeball, pokeballRow, pokeballCol);

GridPane.setHalignment(pikachu, HPos.CENTER);
board.add(pikachu, pikachuRow, pikachuCol);

startTask();



public void startTask()
// Create a Runnable
Runnable task = new Runnable()
public void run()
runTask();

;

// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();


public void runTask()
for (int i = 1; i <= 10; i++)
try
board.add(pikachu, 5, 5);
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();




public void setPikachu(int r, int c)
board.add(pikachu, r, c);
GridPane.setHalignment(pikachu, HPos.CENTER);









java multithreading javafx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 0:30









Andrew Thompson

153k27163338




153k27163338










asked Nov 13 '18 at 0:15









manzkmanzk

31




31











  • Possible duplicate of stackoverflow.com/questions/21083945/…
    – quant
    Nov 13 '18 at 0:25










  • Explain what "still doesn't work" means when you use Platform.runLater. Are you still getting exceptions?
    – Slaw
    Nov 13 '18 at 1:42
















  • Possible duplicate of stackoverflow.com/questions/21083945/…
    – quant
    Nov 13 '18 at 0:25










  • Explain what "still doesn't work" means when you use Platform.runLater. Are you still getting exceptions?
    – Slaw
    Nov 13 '18 at 1:42















Possible duplicate of stackoverflow.com/questions/21083945/…
– quant
Nov 13 '18 at 0:25




Possible duplicate of stackoverflow.com/questions/21083945/…
– quant
Nov 13 '18 at 0:25












Explain what "still doesn't work" means when you use Platform.runLater. Are you still getting exceptions?
– Slaw
Nov 13 '18 at 1:42




Explain what "still doesn't work" means when you use Platform.runLater. Are you still getting exceptions?
– Slaw
Nov 13 '18 at 1:42












1 Answer
1






active

oldest

votes


















0















I learned that GridPane is not thread safe.




There is no such thing as thread-safe when it comes to JavaFX scene nodes. They are expected to be modified only from JavaFX Application Thread.




I have tried using Platform.runLater as well but it still doesn't work.




You have to place it at the correct part too. If you did Platform.runLater(this::startTask); then it would do nothing.



You should use Platform.runLater(() -> board.....) to modify the scene graph from non JavaFX Application Thread.



Lastly, you really should not use board.add(). Adding a control multiple times is going to throw an IllegalArgumentException saying that there is a duplicate children.



So this is what you should do:



Platform.runLater(() -> board.setConstraints(pikachu, 5, 5));


I am not sure why you are always setting it to row 5 and column 5 though.






share|improve this answer




















  • Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
    – manzk
    Nov 14 '18 at 2:00










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271980%2fchanging-items-in-gridpane-using-a-different-thread%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









0















I learned that GridPane is not thread safe.




There is no such thing as thread-safe when it comes to JavaFX scene nodes. They are expected to be modified only from JavaFX Application Thread.




I have tried using Platform.runLater as well but it still doesn't work.




You have to place it at the correct part too. If you did Platform.runLater(this::startTask); then it would do nothing.



You should use Platform.runLater(() -> board.....) to modify the scene graph from non JavaFX Application Thread.



Lastly, you really should not use board.add(). Adding a control multiple times is going to throw an IllegalArgumentException saying that there is a duplicate children.



So this is what you should do:



Platform.runLater(() -> board.setConstraints(pikachu, 5, 5));


I am not sure why you are always setting it to row 5 and column 5 though.






share|improve this answer




















  • Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
    – manzk
    Nov 14 '18 at 2:00















0















I learned that GridPane is not thread safe.




There is no such thing as thread-safe when it comes to JavaFX scene nodes. They are expected to be modified only from JavaFX Application Thread.




I have tried using Platform.runLater as well but it still doesn't work.




You have to place it at the correct part too. If you did Platform.runLater(this::startTask); then it would do nothing.



You should use Platform.runLater(() -> board.....) to modify the scene graph from non JavaFX Application Thread.



Lastly, you really should not use board.add(). Adding a control multiple times is going to throw an IllegalArgumentException saying that there is a duplicate children.



So this is what you should do:



Platform.runLater(() -> board.setConstraints(pikachu, 5, 5));


I am not sure why you are always setting it to row 5 and column 5 though.






share|improve this answer




















  • Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
    – manzk
    Nov 14 '18 at 2:00













0












0








0







I learned that GridPane is not thread safe.




There is no such thing as thread-safe when it comes to JavaFX scene nodes. They are expected to be modified only from JavaFX Application Thread.




I have tried using Platform.runLater as well but it still doesn't work.




You have to place it at the correct part too. If you did Platform.runLater(this::startTask); then it would do nothing.



You should use Platform.runLater(() -> board.....) to modify the scene graph from non JavaFX Application Thread.



Lastly, you really should not use board.add(). Adding a control multiple times is going to throw an IllegalArgumentException saying that there is a duplicate children.



So this is what you should do:



Platform.runLater(() -> board.setConstraints(pikachu, 5, 5));


I am not sure why you are always setting it to row 5 and column 5 though.






share|improve this answer













I learned that GridPane is not thread safe.




There is no such thing as thread-safe when it comes to JavaFX scene nodes. They are expected to be modified only from JavaFX Application Thread.




I have tried using Platform.runLater as well but it still doesn't work.




You have to place it at the correct part too. If you did Platform.runLater(this::startTask); then it would do nothing.



You should use Platform.runLater(() -> board.....) to modify the scene graph from non JavaFX Application Thread.



Lastly, you really should not use board.add(). Adding a control multiple times is going to throw an IllegalArgumentException saying that there is a duplicate children.



So this is what you should do:



Platform.runLater(() -> board.setConstraints(pikachu, 5, 5));


I am not sure why you are always setting it to row 5 and column 5 though.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 2:02









JaiJai

5,73311231




5,73311231











  • Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
    – manzk
    Nov 14 '18 at 2:00
















  • Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
    – manzk
    Nov 14 '18 at 2:00















Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
– manzk
Nov 14 '18 at 2:00




Hi, thank you for letting me know. Yeah sorry for the confusion I was setting it always to 5,5 only as part of my debugging.
– manzk
Nov 14 '18 at 2:00

















draft saved

draft discarded
















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271980%2fchanging-items-in-gridpane-using-a-different-thread%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3