Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Pane and a Group?

Tags:

In JavaFX, what is the difference between a Pane and a Group? I can't make out any difference.

like image 506
Florian Avatar asked Jun 17 '15 19:06

Florian


People also ask

What is JavaFX scene group?

The JavaFX Group component is a container component which applies no special layout to its children. All child components (nodes) are positioned at 0,0 . A JavaFX Group component is typically used to apply some effect or transformation to a set of controls as a whole - as a group.

What is a StackPane?

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.

What are panes in JavaFX?

Layout panes are containers which are used for flexible and dynamic arrangements of UI controls within a scene graph of a JavaFX application. As a window is resized, the layout pane automatically repositions and resizes the nodes it contains.

Is a VBox a pane?

VBox is a part of JavaFX. VBox lays out its children in form of vertical columns. If the vbox has a border and/or padding set, then the contents will be layed out within those insets. VBox class extends Pane class.


1 Answers

A Group is not resizable (meaning that its size is not managed by its parent in the scene graph), and takes on the union of the bounds of its child nodes. (So, in other words, the local bounds of a Group will be the smallest rectangle containing the bounds of all the child nodes). If it is larger than the space it is allocated in its parent, it will be clipped.

By contrast, a Pane is resizable, so its size is set by its parent, which essentially determine its bounds.

Here is a quick demo. The Group is on top and the Pane below. Both contain a fixed blue square at (100,100) and a green square which is moved by pressing the left/right arrow keys. Note how at the beginning, the blue square appears in the top left corner of the group, because the local bounds of the group start at the top-leftmost point of all its child nodes (i.e. the local bounds of the group extend from (100, 100) right and down). As you move the green rectangles "off screen", the group adjusts its bounds to incorporate the changes, wherever possible, whereas the pane remains fixed.

import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; import javafx.scene.layout.Pane; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage;  public class GroupVsPaneDemo extends Application {      @Override     public void start(Stage primaryStage) {         Pane pane = new Pane();         Group group = new Group();          VBox.setVgrow(group, Priority.NEVER);         VBox.setVgrow(pane, Priority.NEVER);          VBox vbox = new VBox(group, pane);           Rectangle rect1 = new Rectangle(100, 100, 100, 100);         Rectangle rect2 = new Rectangle(100, 100, 100, 100);         Rectangle rect3 = new Rectangle(200, 200, 100, 100);         Rectangle rect4 = new Rectangle(200, 200, 100, 100);         rect1.setFill(Color.BLUE);         rect2.setFill(Color.BLUE);         rect3.setFill(Color.GREEN);         rect4.setFill(Color.GREEN);          group.getChildren().addAll(rect1, rect3);         pane.getChildren().addAll(rect2, rect4);          Scene scene = new Scene(vbox, 800, 800);         scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {             double deltaX ;             switch(e.getCode()) {                 case LEFT:                     deltaX = -10 ;                     break ;                 case RIGHT:                     deltaX = 10 ;                     break ;                 default:                     deltaX = 0 ;             }             rect3.setX(rect3.getX() + deltaX);             rect4.setX(rect4.getX() + deltaX);         });          primaryStage.setScene(scene);         primaryStage.show();     }      public static void main(String[] args) {         launch(args);     } } 
like image 151
James_D Avatar answered Sep 20 '22 12:09

James_D