e2298 e2298 - 6 days ago 4
Java Question

is getChildren on Gridpane sorted? (javafx)

I want to get the node in a GridPane by index, I know you can do this by iterating through getChildren, but I was wondering if there was a less ugly way to do it. If I knew getChildren was sorted, it could be done by

getChildren.get( row*width + column)
, but I need to know if it's sorted in some way before doing that.

Answer

getChildren() simply returns a reference to a List. The order of the elements in that list is determined entirely by the methods you call on it and the order you call them. E.g. calling add(child) will add an element (a Node) to the end of the list, so if all you do is call add repeatedly, the nodes will be arranged in the list in the order you add them. If you call add(index, child) the child node will be added at the specified index (so if you repeatedly call add(0, child) the child nodes will be in the reverse of the order in which you add them. If you call addAll(nodes) the child nodes will be added in the order they appear to the end of the list, etc.

So the bottom line is that you can control the order of the child nodes in the list to be whatever you want it to be.

Comments