22

Looks like there is an issue with setting background colors for panels in JavaFX 8.

I had been trying the below, but none of them set the appropriate background colors.

VBox panel = new VBox();
panel.setAlignment(Pos.TOP_LEFT);

// None of the below work
panel.setStyle("-fx-background-color: #FFFFFF;");
panel.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));

Is there something wrong in the way I am setting the background color? This used to work with earlier versions of JavaFX 2.2.

Thanks.

Sirish V
  • 930
  • 2
  • 12
  • 24

4 Answers4

27
panel.setStyle("-fx-background-color: #FFFFFF;");
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Sirish V
  • 930
  • 2
  • 12
  • 24
  • 13
    Change it to `panel.setStyle("-fx-background-color: #FFFFFF;");` then it will work – smac89 Jan 18 '15 at 02:05
  • 1
    "-fx-background-color" – Sam Orozco Sep 15 '16 at 23:44
  • No offence guys, but please do read the actual question before editing for corrections. I do not remember the exact build when this issue happened but 'panel.setStyle("-fx-background-color: #FFFFFF;");' did not work. In later releases this issue has been fixed. – Sirish V Jun 27 '18 at 20:33
20

Both these work for me. Maybe post a complete example?

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class PaneBackgroundTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        VBox vbox = new VBox();
        root.setCenter(vbox);

        ToggleButton toggle = new ToggleButton("Toggle color");
        HBox controls = new HBox(5, toggle);
        controls.setAlignment(Pos.CENTER);
        root.setBottom(controls);

//        vbox.styleProperty().bind(Bindings.when(toggle.selectedProperty())
//                .then("-fx-background-color: cornflowerblue;")
//                .otherwise("-fx-background-color: white;"));

        vbox.backgroundProperty().bind(Bindings.when(toggle.selectedProperty())
                .then(new Background(new BackgroundFill(Color.CORNFLOWERBLUE, CornerRadii.EMPTY, Insets.EMPTY)))
                .otherwise(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY))));

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
James_D
  • 201,275
  • 16
  • 291
  • 322
7

Try this one in your css document,

-fx-background-color : #ffaadd;

or

-fx-base : #ffaadd; 

Also, you can set background color on your object with this code directly.

yourPane.setBackground(new Background(new BackgroundFill(Color.DARKGREEN, CornerRadii.EMPTY, Insets.EMPTY)));
c0der
  • 18,467
  • 6
  • 33
  • 65
Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
0

This code snippet could also be directly implement

yourPane.setBackground(new Background(new BackgroundFill(Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY)));