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 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:
- 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