简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM