I want to emulate the swapping of array elements in a GUI in javafx.
I have a grid pane Memory
which acts like an array. Row 1 of the grid only has the array.
In each row/col I have added a stack pane having image view and label.
I want to change the image view of the current node and its next node. Along with the data.
I tried doing it through the loop but the result was not seen. So I used a thread to do the background image swapping but that did not allow swapping of label values. This is the closest I have got without swapping while changing the background.
public void insertFront() {
System.out.println("InsertList before insert "+ insertList.size()+" : "+insertList);
if (insertList.size() == 0){
ObservableList<Node> nodes = insertArr[0].getChildren();
Label temp = (Label) nodes.get(1);
insertList.add(random.nextInt(999));
temp.setText(""+insertList.get(0));
} else {
if (insertList.size() < max) {
new Thread(()->{
for (int i = insertList.size()-1; i >= 0; i--) {
ObservableList<Node> currNodes = insertArr[i].getChildren();
ImageView temp = (ImageView) currNodes.get(0);
temp.setImage(current);
ObservableList<Node> nextNodes = insertArr[i+1].getChildren();
ImageView tempNext = (ImageView) nextNodes.get(0);
tempNext.setImage(next);
Label tempLabel = (Label) currNodes.get(1);
Label tempNextLabel = (Label) nextNodes.get(1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
temp.setImage(allocated);
tempNext.setImage(allocated);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
for (int i = insertList.size()-1; i >= 0; i--) {
ObservableList<Node> currNodes = insertArr[i].getChildren();
ObservableList<Node> nextNodes = insertArr[i+1].getChildren();
Label tempLabel = (Label) currNodes.get(1);
Label tempNextLabel = (Label) nextNodes.get(1);
tempNextLabel.setText(tempLabel.getText());
tempLabel.setText("");
}
ObservableList<Node> nodes = insertArr[0].getChildren();
Label temp = (Label) nodes.get(1);
insertList.add(0, random.nextInt(999));
temp.setText("" + insertList.get(0));
} else {
insertList.removeAll(insertList);
fillInsert();
insertFront();
}
}
System.out.println("InsertList after insert "+ (insertList.size())+" : "+insertList);
}
But this is not synced. What I was trying to achieve is at least show the swapping of backgrounds first and then directly update the row. but because the threads are not synced the transition is slow and the grid row is updated.
I want help for at least syncing the threads so the transition is shown first. Then the row is changed.