Search code examples
scalascalafx

HBox not resizing in ScalaFX


I've got a simple UI consisting of an HBox with two elements (here, labels, but I've had the same issue with a TreeView.

  stage = new PrimaryStage {
    title = "Simple UI"
    scene = new Scene {
      content = new HBox() {
        children = Seq(
          new Label("Left"),
          new Label("Right")
        )
        resizable = true
        hgrow = Priority.Always
        vgrow = Priority.Always

        padding = Insets(15.0)

        maxHeight = Double.MaxValue
        maxWidth = Double.MaxValue
        style = " -fx-border-width : 1px; -fx-border-color : #000000"
      }
    }
  }

The HBox doesn't seem to resize when the window is resized - I've used CSS to see the border of the HBox. This has allowed me to confirm that the size of the HBox itself remains unchanged as the window size changes.

What am I missing?


Solution

  • And, as it turns out - I figure it out just after I post. If I set the HBox as the root (instead of as the content) it suddenly is always resized to fit the window, and the elements contained within are resized as intended.

      stage = new PrimaryStage {
        title = "Simple UI"
        scene = new Scene {
          root = new HBox() {
            children = Seq(
              new Label("Left"),
              new Label("Right")
            )
            resizable = true
            hgrow = Priority.Always
            vgrow = Priority.Always
    
            padding = Insets(15.0)
    
            maxHeight = Double.MaxValue
            maxWidth = Double.MaxValue
            style = " -fx-border-width : 1px; -fx-border-color : #000000"
          }
        }
      }