Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJavaFX | Group Class

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:

  1. Group(): Constructs a new group.
  2. Group(Collection children): Constructs a new group with specified nodes.
  3. 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:

  1. 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 stage
    import 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:

  2. 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 stage
    import 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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?