JavaFX | Group Class

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.
 
Commonly Used Methods:
| Method | Explanation | 
|---|---|
| getChildren() | Returns the children of the group. | 
| isAutoSizeChildren() | Gets the value of the property autoSizeChildren. | 
| minHeight(double width) | Returns the node’s minimum height for use in layout calculations. | 
| minWidth(double height) | Returns the node’s minimum width for use in layout calculations. | 
| prefHeight(double width) | Group defines the preferred height as simply being the height of its layout bounds. | 
| prefWidth(double height) | Group defines the preferred width as simply being the width of its layout bounds. | 
| setAutoSizeChildren(boolean v) | Sets the value of the property autoSizeChildren. | 
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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;importjavafx.scene.shape.*;publicclassGroup_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("Group");// create a GroupGroup group =newGroup();// create a labelLabel label =newLabel("this is Group example");// add label to groupgroup.getChildren().add(label);// circleCircle c =newCircle(100,100,30);// add Circle to Groupgroup.getChildren().add(c);// create a sceneScene scene =newScene(group,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;importjavafx.scene.shape.*;publicclassGroup_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("Group");// create a GroupGroup group =newGroup();// create a labelLabel label =newLabel("this is Group example");// add label to groupgroup.getChildren().add(label);// circleCircle c =newCircle(50,50,30);// set auto resizegroup.setAutoSizeChildren(true);// add Circle to Groupgroup.getChildren().add(c);// create a sceneScene scene =newScene(group,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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
				
					



