2

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?

user3603546
  • 325
  • 2
  • 11

2 Answers2

2

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

.scroll-pane .viewport {
    -fx-background-color: red;
}
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
1
sp.setStyle("-fx-background: blue")

instead of:

sp.setStyle("-fx-background-color: blue")
Philip Vaughn
  • 606
  • 6
  • 20