JavaFX Background not beeing used on a ScrollPane element [Solved]
-
In the following example the setBackground() for the TextField element won't work if it is in a Pane inside a ScrollPane which has the style "-fx-background: transparent;" added, but the problem is that I need it, in order to make the ScrollPane transparent. The weird part is that setBackground() works just fine for the Label element I used in this example. I can not use a CSS file in my original project. Is this a bug or is there a walk-around for this problem? To test it, just change the following line
Scene scene = new Scene(scroll);
in this:
Scene scene = new Scene(pane);
public class TestingTextFieldBackground extends Application {
public static void main(String\[\] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Pane pane = new AnchorPane(); pane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY))); pane.setPrefWidth(500); pane.setPrefHeight(500); pane.setLayoutX(0); pane.setLayoutY(0); ScrollPane scroll = new ScrollPane(); scroll.setContent(pane); scroll.setLayoutX(0); scroll.setLayoutY(100); scroll.setPrefWidth(500); scroll.setPrefHeight(400); scroll.setHbarPolicy(ScrollBarPolicy.NEVER); scroll.setVbarPolicy(ScrollBarPolicy.ALWAYS); scroll.setStyle( "-fx-background: transparent; -fx-background-color: transparent; -fx-padding: 0; -fx-background-insets: 0;"); Scene scene = new Scene(scroll); scene.setFill(Color.TRANSPARENT); Stage stage = new Stage(); stage.setWidth(500); stage.setHeight(500); stage.initStyle(StageStyle.TRANSPARENT); stage.setScene(scene); Label label = new Label("Bellow you'll find our clients data. (This text is used just as an example)"); label.setLayoutX(0); label.setLayoutY(0); label.setTextFill(Color.YELLOW); // This will work fine even if ScrollPane is used. label.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY))); pane.getChildren().add(label); int up = 100; for (int i = 0; i < 50; i++) { TextField text = new TextField("Client with ID: " + i + ", has " + up + " dollars in account."); text.setLayoutX(0); text.setLayoutY(up); text.setPrefWidth(450); // The problem is here. text.setBackground(new Background(new BackgroundFill(Color.DARKRED, CornerRadii.EMPTY, Insets.EMPTY))); pane.getChildren().add(text); up += 80; } stage.centerOnScreen(); stage.show(); }
-
In the following example the setBackground() for the TextField element won't work if it is in a Pane inside a ScrollPane which has the style "-fx-background: transparent;" added, but the problem is that I need it, in order to make the ScrollPane transparent. The weird part is that setBackground() works just fine for the Label element I used in this example. I can not use a CSS file in my original project. Is this a bug or is there a walk-around for this problem? To test it, just change the following line
Scene scene = new Scene(scroll);
in this:
Scene scene = new Scene(pane);
public class TestingTextFieldBackground extends Application {
public static void main(String\[\] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Pane pane = new AnchorPane(); pane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY))); pane.setPrefWidth(500); pane.setPrefHeight(500); pane.setLayoutX(0); pane.setLayoutY(0); ScrollPane scroll = new ScrollPane(); scroll.setContent(pane); scroll.setLayoutX(0); scroll.setLayoutY(100); scroll.setPrefWidth(500); scroll.setPrefHeight(400); scroll.setHbarPolicy(ScrollBarPolicy.NEVER); scroll.setVbarPolicy(ScrollBarPolicy.ALWAYS); scroll.setStyle( "-fx-background: transparent; -fx-background-color: transparent; -fx-padding: 0; -fx-background-insets: 0;"); Scene scene = new Scene(scroll); scene.setFill(Color.TRANSPARENT); Stage stage = new Stage(); stage.setWidth(500); stage.setHeight(500); stage.initStyle(StageStyle.TRANSPARENT); stage.setScene(scene); Label label = new Label("Bellow you'll find our clients data. (This text is used just as an example)"); label.setLayoutX(0); label.setLayoutY(0); label.setTextFill(Color.YELLOW); // This will work fine even if ScrollPane is used. label.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY))); pane.getChildren().add(label); int up = 100; for (int i = 0; i < 50; i++) { TextField text = new TextField("Client with ID: " + i + ", has " + up + " dollars in account."); text.setLayoutX(0); text.setLayoutY(up); text.setPrefWidth(450); // The problem is here. text.setBackground(new Background(new BackgroundFill(Color.DARKRED, CornerRadii.EMPTY, Insets.EMPTY))); pane.getChildren().add(text); up += 80; } stage.centerOnScreen(); stage.show(); }
You need a CSS file with the following lines:
.scroll-pane > .viewport {
-fx-background-color: transparent;
}This will make the gray area transparent. After that if you want to make the whole ScrollPane transparent, then set it a background with .setBackground() function. Like this, .setBackground() will work just fine for the nodes that you have problem with.