I made an app where you search for numbers in a specific order. This is how it looks :
Currently, I have put the numbers in a specific order. How could I randomize this? For example, I want each button to change all the location of the buttons everytime a button is clicked. Problem is that GridPane can stack the nodes in one place, making the buttons hide behind eachother. Is there a way to set that the nodes cannot stack?
On the other hand , I found a method from a post where someone wanted to switch the location of two buttons. The code looked like this :
public static void swap(Node button1, Node button2) {
Integer temp = GridPane.getRowIndex(button1);
GridPane.setRowIndex(button1, GridPane.getRowIndex(button2));
GridPane.setRowIndex(button2, temp);
temp = GridPane.getColumnIndex(button1);
GridPane.setColumnIndex(button1, GridPane.getColumnIndex(button2));
GridPane.setColumnIndex(button2, temp);
}
I tried to make an arraylist which contains the nodes, and then using math.random to get a random index from the arraylist , which then gets a random button.
public static ArrayList<Node> buttonList = new ArrayList<Node>();
and the code to randomize the index :
public static int maxListValue = 19;
public static int minListValue = 0;
public static int listRandom = (int) (Math.random() * maxListValue) + minListValue;
Then, using the swap method, I tried :
swap(buttonList.get(listRandom), buttonList.get(listRandom));
this doesn't work for some reason, the button does nothing when I put random on the first node, however, if I change it to specify the first node , like : swap(buttonOne, buttonList.get(listRandom));
then it works, but calling the method for multiple buttons does not work, it only works on the first line of the method.
Would appreciate any tips on why this is not working
Circle
withButton
.\ – Sedrick