简体   繁体   中英

How to change the background of a ScrollPane (JavaFX/ScalaFX)?

I want to change the background color of a ScrollPane. This is part of my code where I try to do that:

    val sp=new javafx.scene.control.ScrollPane(new Group(new Text(...)))
    sp.setPannable(true)
    sp.setStyle("-fx-background-color: blue")
    sp.setBackground(new Background(Array(new BackgroundFill(Color.DARKCYAN,new CornerRadii(0),Insets(0)))))

Text appears OK, but both attempts to change the background color have no effect, using:
Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05).

Inspecting with Scenic View , I discover that two StackPanes have unexpectedly appeared in the scene graph below the ScrollPane, so the hierarchy is:

  1. ScrollPane //which I created
  2. StackPane //UNEXPECTED -- clips the content
  3. StackPane //UNEXPECTED -- full size content
  4. Group //which I created
  5. Text //which I created

If I change the background of either of the StackPane-s to, say, "-fx-background-color: blue" (with Scenic View ), it has effect, but not the style of the ScrollPane. But how to do that from code? If I do

println(sp.content())

, it says

Group@567fa81a

Is there a simple way to access the StackPanes or change the background? I could "slap in" a big filled rectangle, but that seems ugly and complicates resizing, what is wrong with the background proper?

Similar to JScrollPane , JavaFX ScrollPane has a StackPane container within itself called viewport . So to set background for your ScrollPane use this CSS rule (ie to set red background color):

.scroll-pane .viewport {
    -fx-background-color: red;
}
sp.setStyle("-fx-background: blue")

代替:

sp.setStyle("-fx-background-color: blue")

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