Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Setting JavaFX 8 Scene Fill/Color on OSX

AdamBJun 10 2014 — edited Jun 13 2014

No problem setting background color for layouts, e.g.

bdrPn.setBackground(new Background((new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY))));

But neither of the following are working for me, running JavaFX 8 on latest OS-X

scene = new Scene(bdrPn, winW, winH, Color.BLACK);

scene.setFill(Color.BLACK);

scene.setFill() worked fine for previous versions of JavaFX. Any advice?

Comments

David Grieve-Oracle

What I think you are seeing is the background color of your BorderPane (I'm assuming bdrPn is a BorderPane). Try this bit of CSS to see if it doesn't help;

BorderPane bdrPn = new BorderPane();

bdrPn.setStyle("-fx-background-color: transparent;");

AdamB

Hi David,

Thanks for your suggestion, but no, JavaFX 8 Scene.setFill( ) still apparently not working.

As a workaround, to keep the default garish WHITE scene covered, I shall color my layouts, size them to full extent of app window & add Stage width/height responders. E.g.

stg.widthProperty().addListener(

          new ChangeListener<Number>() {

                    @Override

                    public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {

                        winW = stg.getWidth();

                        bdrPn.setPrefWidth(winW);

                        bdrPn.setMinWidth(Control.USE_PREF_SIZE);

                        flowPn.setPrefWidth(winW);

                        flowPn.setMinWidth(Control.USE_PREF_SIZE);

                    }

                });

This appears an adequate solution for my purpose. Be nice just to color the Scene/background though...

David Grieve-Oracle

You really don't need to do all that. Just set the root of your scene to be a StackPane.

James_D

This works as expected for me on JDK 1.8.0_05 Mac OSX 10.9.3

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.layout.BorderPane;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class FilledSceneTest extends Application {

  @Override

  public void start(Stage primaryStage) {

  BorderPane root = new BorderPane();

  Scene scene = new Scene(root, 600, 400, Color.BLACK);

  primaryStage.setScene(scene);

  primaryStage.show();

  }

  public static void main(String[] args) {

  launch(args);

  }

}

AdamB

Hi James,

Many thanks for your code snippet, which prompted me to isolate bug.

Simply instancing ScollPane provokes "whiteout", even before it's added (?)

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.layout.BorderPane;

import javafx.scene.control.ScrollPane;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class FilledSceneTest extends Application {

  @Override

  public void start(Stage primaryStage) {

  BorderPane root = new BorderPane();

  Scene scene = new Scene(root, 600, 400, Color.BLACK);

  ScrollPane scrollPn = new ScrollPane(); // *** THIS LINE IS PROBLEM ***

  primaryStage.setScene(scene);

  primaryStage.show();

  }

  public static void main(String[] args) {

  launch(args);

  }

}

jsmith

I've seen something like this before on another forum thread I replied to on this forum in the last six weeks or so.

Maybe search and you might find more info.

jsmith

After a bit of searching I think I dug up the related post:

My comment on that post was =>

I guess initializing any control triggers something in the css setup which engages background colors for panes, and that same css is not triggered if no control is ever created in the app.  That is just speculation.  It is a pure edge case because there would be few JavaFX applications without any controls at all.

I don't know why things work this way.

David Grieve-Oracle

This happens because modena.css sets the background color of the root node. Setting the style -fx-background-color: transparent on the root node of the scene is the solution.

        BorderPane root = new BorderPane();
        root.setStyle("-fx-background-color: transparent;");

        Scene scene = new Scene(root, 600, 400, Color.BLACK);
jsmith

It is weird that this behaviour (the loading and use of the modena default user agent stylesheet) is conditional on some control like ScrollPane being instantiated.  Adam's sample demonstrates it pretty well.  It's probably a pretty minor thing as any non-trivial app will have controls in it.

David Grieve-Oracle

The default user agent stylesheet is loaded the first time a Control is instantiated. The reason for this is to avoid loading stylesheets and reduce CSS overhead in scene-graphs that contain nodes that don't use CSS.

jsmith

Oh that makes sense, it's subtle but it makes sense.  Now I understand what is going on and why.

I guess the other way this could have been handled is similar to the depthBuffer and antialias settings in the Scene constructor, there could be a setting which allows you to create scenes which use CSS and scenes which don't.

Thanks for the info David.

AdamB

Thanks for your interest here David.

I'm still struggling to see the merit of the default stylesheet loading you describe, since it does appear to (literally) render Scene.setFill() redundant -- Almost every app will have Controls, yet once they're instanced the default behavior is to obscure the Scene/base color unless I then set overlain Layouts transparent? I'm still perplexed why this is a useful feature and why only now introduced -- Did Scene.setFill()+Control elements not work more intuitively before the FX8 upgrade?

jsmith

I think the default caspian stylesheet in JavaFX 2 didn't set background colors for standard panes like BorderPane, so you didn't need the extra step required by the default Java 8 modena.css of setting the pane background color to null or transparent in order to see the scene fill.  This resulted in some unexpected behaviour in Java 8 for me with stages that were intended to be transparent because I had to additionally set the root pane fill to allow the transparency to work.  I think this is one of the reasons why Java 8 provided a way to switch the default stylesheet between caspian and modena, so if you have a "legacy" app which was relying on the default caspian styling, you could switch your default stylesheet to capsian and the app would still look as before (at least in theory).  I think modena sets a default background color for panes so that they will be opaque and have a consistent background color which can be modified in the style, once you know that modena works this way, then you can reason about it OK I think.

David Grieve-Oracle

The history behind it is that the designer of the modena theme felt that the controls looked better on this particular  background color, which is a very light grey. Unfortunately, the Scene's background fill cannot be styled from CSS, so the style was set on the root node of the scene. There is an issue logged in JIRA to make Scene so that it can be styled by CSS (RT-31282)

The merit of loading in this way is to avoid css overhead in scene's that don't use controls. This would be typical of a splash screen, for example. Or maybe a game. This design choice was made a long time ago when CSS performance was a big issue, but it still makes sense for embedded devices.

1 - 14
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 11 2014
Added on Jun 10 2014
14 comments
19,288 views