Marco
Marco

Reputation: 2235

ScalaFX : How to stretch ScrollPane to fit its parent

I use the Border Pane as a layout. The bottom child has a ScrollPane, which should use the complete width (stretch) of the Border Pane - regardless of its content.

val scrollPane = new ScrollPane() {
hbarPolicy = ScrollBarPolicy.ALWAYS
content = new TextFlow() {
  children.add(new Text("Hello World"))
}
}

stage = new PrimaryStage {
title = "ScalaFX Hello World"
width = 1024
height = 768
scene = new Scene {
  content = new BorderPane() {
    center = Label("This is my center text")
    bottom = new Pane {
      children.add(scrollPane)
    }
  }
}

It looks during runtime the following way:

During Runtime

Any chance I can achieve this without manually setting the width of the ScrollPane ?

Upvotes: 1

Views: 582

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36742

In ScalaFX, unless a parent is passed, Scene is instantiated with an empty Group.

class Scene(override val delegate: jfxs.Scene = new jfxs.Scene(new jfxs.Group())) 

So, instead of setting content, set the root of the Scene.

scene = new Scene {
    root = new BorderPane() {
    center = Label("This is my center text")
    bottom = scrollPane
  }
}

You must have noticed that I even removed the new Pane that you were adding to bottom before adding the ScrollPane.

MCVE

import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.control.ScrollPane.ScrollBarPolicy
import scalafx.scene.control.{Label, ScrollPane}
import scalafx.scene.layout.BorderPane
import scalafx.scene.text.{Text, TextFlow}

object Main extends JFXApp {

  val scrollPane = new ScrollPane() {
    hbarPolicy = ScrollBarPolicy.ALWAYS
    content = new TextFlow() {
      children.add(new Text("Hello World"))
    }
  }
  stage = new JFXApp.PrimaryStage {
    title.value = "Hello Stage"
    width = 200
    height = 150
    scene = new Scene {
        root = new BorderPane() {
        center = Label("This is my center text")
        bottom = scrollPane
      }
    }
  }
}

Screenshot

enter image description here

Upvotes: 2

Related Questions