what is the difference between stackpane and root in javafx?


I was practicing for javafx for doing pie chart. Following are the codes for developing pie chart. If i do with the Group and with the StackPane,I find no difference in the output.I have commented the Group part.Just wandering the difference between the two.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChartApp1 extends Application {
public static void main(String[] args) {
public void start(Stage primaryStage) {
PieChart pieChart = new PieChart();
//Group p=new Group();

StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 400, 250));
private ObservableList<PieChart.Data> getChartData() {
ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
answer.addAll(new PieChart.Data("java", 17.56),
new PieChart.Data("C", 17.06),
new PieChart.Data("C++", 8.25),
new PieChart.Data("C#", 8.20),
new PieChart.Data("ObjectiveC", 6.8),
new PieChart.Data("PHP", 6.0),
new PieChart.Data("(Visual)Basic", 4.76),
new PieChart.Data("Other", 31.37));
return answer;
8/4/2016 3:43:32 AM

Accepted Answer

According to the official documentation,

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.

The stackpane will attempt to resize each child to fill its content area. If the child could not be sized to fill the stackpane (either because it was not resizable or its max size prevented it) then it will be aligned within the area using the alignment property, which defaults to Pos.CENTER

While the official documentation for the Group class states that

A Group node contains an ObservableList of children that are rendered in order whenever this node is rendered. A Group will take on the collective bounds of its children and is not directly resizable.

Any transform, effect, or state applied to a Group will be applied to all children of that group. Such transforms and effects will NOT be included in this Group's layout bounds, however if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.

By default, a Group will "auto-size" its managed resizable children to their preferred sizes during the layout pass to ensure that Regions and Controls are sized properly as their state changes. If an application needs to disable this auto-sizing behavior, then it should set autoSizeChildren to false and understand that if the preferred size of the children change, they will not automatically resize.

8/4/2016 3:42:47 AM

StackPane is a container object; it is used to lay out nodes in a specific manner (back-to-front, according to z-order). Functionally, StackPane is a layout manager. The term root or root node refers to the top-most node in the scene graph of a JavaFX application. The root node is passed to the constructor of the Scene; it is the only node that does not have a parent. Group is a container too. It is not a layout manager; it places its children in absolute coordinates. StackPane and Group can be root nodes.

You would see a difference between Group and StackPane if you wanted to place your chart at a specific location in the application's client area. Placing a chart at x=10 and y=10 would work with Group but would not work with StackPane.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow