简体   繁体   中英

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:

在运行期间

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM