TreeShowing class provides access to the internal "treeShowing" property of
JavaFX Nodes. This property is essential for determining whether a
Node is currently part of the scene graph and actively being displayed.
In JavaFX, a Node eligible for garbage collection might still be retained in memory
due to ongoing animations or background tasks. The internal "treeShowing" property
tracks the visibility and usage of each Node, but it is not exposed through the standard
JavaFX API. The TreeShowing class bridges this gap, enabling developers to monitor
a node's lifecycle and manage resources effectively to prevent memory leaks.
By utilizing the TreeShowing class, developers can:
- Detect when a
Nodeis removed from the scene graph. - Stop associated animations or background tasks to free up resources.
This facilitates the creation of robust, memory-efficient JavaFX applications by ensuring that unused nodes do not retain unnecessary resources.
Example Usage:
import javafx.animation.Timeline;
import javafx.scene.Node;
public class Example {
public void setupNode(Node myNode) {
// Create a Timeline animation
Timeline myTimeline = new Timeline();
myTimeline.setCycleCount(Timeline.INDEFINITE);
// Obtain the TreeShowing instance for the node
TreeShowing treeShowing = TreeShowing.treeShowing(myNode);
// Add a listener to respond to changes in the treeShowing property
treeShowing.addListener((observable, oldValue, showing) -> {
if (showing) {
// Start the animation when the node is part of the scene graph
myTimeline.play();
} else {
// Stop the animation when the node is removed from the scene graph
myTimeline.stop();
}
});
// Immediately start the animation if the node is already showing
if (treeShowing.get()) {
myTimeline.play();
}
}
}
In this example, the listener monitors the "treeShowing" property of myNode.
When myNode is added to the scene graph, the animation starts. Conversely, when
myNode is removed, the animation stops, ensuring that resources are managed
efficiently.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisTreeShowing(javafx.scene.Node node) static javafx.beans.property.BooleanPropertytreeShowing(javafx.scene.Node node)
-
Constructor Details
-
TreeShowing
public TreeShowing()
-
-
Method Details
-
treeShowing
public static javafx.beans.property.BooleanProperty treeShowing(javafx.scene.Node node) -
isTreeShowing
public static boolean isTreeShowing(javafx.scene.Node node)
-