WycliffeAssociates / otter-jvm

Desktop JVM repository for the Otter project
GNU General Public License v3.0
6 stars 1 forks source link

Spike: JavaFX Layouts #436

Open jsarabia opened 5 years ago

jsarabia commented 5 years ago

Compare and contrast the following layouts: Pane, Region, Hbox/Vbox, Flowpane, Borderpane, Anchorpane

For each layout: Can you align child nodes? Can you resize? If yes, how?

What is reflow and how does it affect the UI?

KJoslyn commented 5 years ago

Region is the base class for all Node-based UI controls.

  1. You cannot directly add children to a region since Region only has an unmodifiable list of children via the public API. However, it looks like tornadofx makes it possible to add children using the extension method add on EventTarget, or by adding children via tornadofx's lambda builder style.
  2. Yes, Region can be resized by using prefWidth, prefHeight, or prefSize, etc. Interestingly, if using hgrow = Priority.ALWAYS, the height defaults to 0 if there are no children.

Pane extends Region and is the base class of all layouts in JavaFX.

  1. Child nodes can be added by using pane.getChildren().add() or addAll().
  2. The parent will resize the pane within the pane's resizable range (you can easily set the preferred size by setPrefSize(X,Y). Pane also has a minimum, preferred, and maximum width and height. Inset sizes determine the minimum dimensions. Preferred width height is calculated by taking: Inset sizes + min width/height required to encompass each child at its x/y location and preferred width/height

HBox/VBox extends Pane.

New features beyond Pane:

Important: alignment and isFillHeight/isFillWidth affect the layout of the children in the HBox/VBox, while min/pref/maxWidth/Height set the HBox/VBox size. vgrow/hgrow are settings on the individual children.

FlowPane extends Pane. Need to specify an orientation (vertical or horizontal).

  1. Children are added in a flow that wraps either vertically or horizontally at the flowpane boundary
  2. FlowPane's parent can resize the FlowPane. For a horizontal FlowPane, prefWidth = left/right insets + prefWrapLength; prefHeight = top/bottom insets + height required to display all children at their preferred heights when wrapped at a specified width.

BorderPane extends Pane.

  1. Child nodes are added by adding them to regions inside the BorderPane (top, left, right, bottom, center)
  2. BorderPane can be resized by the parent. It is often used as the root of a Scene- in this case, its size = the size of the scene. Otherwise, the parent resizes the borderpane.

AnchorPane extends Pane. It allows the edges of child nodes to be anchored to an offset from the pane's edges.

  1. Children are added by setting anchor points for the children (can set one or more of: topAnchor, leftAnchor, bottomAnchor, rightAnchor).
  2. The parent can resize the anchorpane according to the anchorpane's contents.
    • If there is a border or padding set, offsets are measured from the inside edge of the insets.
    • If anchors are provided on opposite sides (and the child is resizable), the child will be resized to maintain the offsets; otherwise the anchor pane will resize the child to its preferred size.

Reflow is the re-calculation of positions and dimensions of all elements. Commonly, if the user resizes the window, UI elements may be either resized or moved to better accommodate the new window size. For example, if the screen width decreases, elements may be stacked vertically on top of eachother; if the screen width increases, elements may move next to each other horizontally in rows.

Other reflow triggers are:

Reflow can be expensive and blocks the UI thread. To mitigate reflow,