Another short tip: Swing provides a layout manager called CardLayout, which manages a set of components (cards) inside a container but always only shows one of them. The method CardLayout.show(Container, String) allows to switch between the components / the cards.
The same behaviour can be accomplished in JavaFX by using the StackPane, adding several children (each using the entire width and height of the pane) and calling the Node.toFront() method to switch between the children. However, there is one big difference: the StackPane will always layout all of its children, independent of wether they are currently showing or not. This might result in bad performance of your application and can be noticed when resizing the window that contains the pane.
My advice: manage your “cards” by adding them to or removing them from the scene graph. These operations are fast and flicker-free (this is JavaFX in Java 8, not Swing before Java 6).
Interesting! Another option could be use the managedProperty for the children not on top of the Stack, that prevents their layout.
Reblogged this on Dinesh Ram Kali..
[…] put up two more tips this week. Tip #13 is to study the Modena CSS file, and tip #14 is about StackPane children, or as he puts it, ‘hidden but not gone’. I should note that in this case, Gilberto has […]
Gilberto: regarding the “managed” property: this works, too, but didn’t we both just recently noticed that unmanaged node can have some unwanted side-effects that are hard to debug? For example the SplitPane divider location not being updated correctly when one of the sides contained an unmanaged node.
Dirk: Yes, you’re right… but I guess then the side-effects are just bugs. For example a toolbar in FX, when it has some of the items unmanaged, their size is still considered to show the >> button when the toolbar gets small…