JavaFX | Group Class - GeeksforGeeks

10 Sep, 2018
Group class is a part of JavaFX. A Group contains the number of nodes. A Group will take on the collective bounds of its children and is not directly resizable. Group class inherits Parent class.
Constructor of the class:
Group(): Constructs a new group.Group(Collection children): Constructs a new group with specified nodes.Group(Node... c): Constructs a new group with specified nodes.
Group(): Constructs a new group.
Group(Collection children): Constructs a new group with specified nodes.
Group(Node... c): Constructs a new group with specified nodes.
Commonly Used Methods:
Below programs illustrate the use of Group class:
Java Program to create a Group and add it to the stage: In this program we are creating a Label named label, and a Circle named circle. Now create a Group name group and add the label and circle to it by using the getChildren().add() function. Create a scene and add the group to the scene. Add the scene to the stage and display the stage to view the final results.// Java Program to create a Group// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class Group_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Group");              // create a Group            Group group = new Group();              // create a label            Label label = new Label("this is Group example");              // add label to group            group.getChildren().add(label);              // circle            Circle c = new Circle(100, 100, 30);              // add Circle to Group            group.getChildren().add(c);              // create a scene            Scene scene = new Scene(group, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Java Program to create a Group, set auto resize to true and add it to the stage: In this program we are creating a Label named label and a Circle named circle. Then we will create a Group name group and add the label and circle to it by using the getChildren().add() function. Set the auto size children to true using the setAutoSize() function. Create a scene and add the group to the scene. Add the scene to the stage and display the stage to view the final results.// Java Program to create a Group,// set auto resize to true// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class Group_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Group");              // create a Group            Group group = new Group();              // create a label            Label label = new Label("this is Group example");              // add label to group            group.getChildren().add(label);              // circle            Circle c = new Circle(50, 50, 30);              // set auto resize            group.setAutoSizeChildren(true);              // add Circle to Group            group.getChildren().add(c);              // create a scene            Scene scene = new Scene(group, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
Java Program to create a Group and add it to the stage: In this program we are creating a Label named label, and a Circle named circle. Now create a Group name group and add the label and circle to it by using the getChildren().add() function. Create a scene and add the group to the scene. Add the scene to the stage and display the stage to view the final results.// Java Program to create a Group// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class Group_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Group");              // create a Group            Group group = new Group();              // create a label            Label label = new Label("this is Group example");              // add label to group            group.getChildren().add(label);              // circle            Circle c = new Circle(100, 100, 30);              // add Circle to Group            group.getChildren().add(c);              // create a scene            Scene scene = new Scene(group, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a Group// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class Group_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Group");              // create a Group            Group group = new Group();              // create a label            Label label = new Label("this is Group example");              // add label to group            group.getChildren().add(label);              // circle            Circle c = new Circle(100, 100, 30);              // add Circle to Group            group.getChildren().add(c);              // create a scene            Scene scene = new Scene(group, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Java Program to create a Group, set auto resize to true and add it to the stage: In this program we are creating a Label named label and a Circle named circle. Then we will create a Group name group and add the label and circle to it by using the getChildren().add() function. Set the auto size children to true using the setAutoSize() function. Create a scene and add the group to the scene. Add the scene to the stage and display the stage to view the final results.// Java Program to create a Group,// set auto resize to true// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class Group_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Group");              // create a Group            Group group = new Group();              // create a label            Label label = new Label("this is Group example");              // add label to group            group.getChildren().add(label);              // circle            Circle c = new Circle(50, 50, 30);              // set auto resize            group.setAutoSizeChildren(true);              // add Circle to Group            group.getChildren().add(c);              // create a scene            Scene scene = new Scene(group, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a Group,// set auto resize to true// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class Group_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Group");              // create a Group            Group group = new Group();              // create a label            Label label = new Label("this is Group example");              // add label to group            group.getChildren().add(label);              // circle            Circle c = new Circle(50, 50, 30);              // set auto resize            group.setAutoSizeChildren(true);              // add Circle to Group            group.getChildren().add(c);              // create a scene            Scene scene = new Scene(group, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Group.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Stack Class in Java
Set in Java
Singleton Class in Java
Overriding in Java