Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. JavaFX Background not beeing used on a ScrollPane element [Solved]

JavaFX Background not beeing used on a ScrollPane element [Solved]

Scheduled Pinned Locked Moved Java
helpcsstutorialquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valentinor
    wrote on last edited by
    #1

    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();
    }
    
    V 1 Reply Last reply
    0
    • V Valentinor

      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();
      }
      
      V Offline
      V Offline
      Valentinor
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups