Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does instantiating a JavaFX scene control corrupt the layout

Tags:

java

javafx

I'm trying to create a simple data entrty application with javafx and ran into a problem when adding a scene control. The display loses it's fill colour even BEFORE i've added the control to the scene! Simply instantiating the control breaks it.

I was running on Oracle java 8 on windows, but I;ve tried OpenJDK 8 on Windows and OpenJDk/OpenJFX 13 on linux and all behave identically. I stripped out the code to the bear minmium to recreate the problem.

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class BasicTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Rectangle r = new Rectangle();
        r.setWidth(200);
        r.setHeight(50);
        r.setFill(Color.BLUE);
        r.setStroke(Color.WHITE);
        r.setStrokeWidth(2);

        Text t = new Text();
        t.setText("Confirm");
        t.setFill(Color.WHITE);
        t.setFont(Font.font("null", 40));

        StackPane sp = new StackPane(r);
        sp.getChildren().add(t);
        sp.setMaxWidth(200);

        t.setTranslateY(-2);

        Label b = new Label("Click me");//Comment this line out after first run

        Scene scene = new Scene(sp, 300, 200);
        scene.setFill(Color.BLUE);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

With the label commented out the scene background is blue so I get a white "Confirm" with white outline. Just adding the label constructor will make the scene background go grey.

like image 350
Andrew Waters Avatar asked Nov 07 '19 17:11

Andrew Waters


People also ask

Can a canvas be used in a layout JavaFX?

Because the Canvas is a Node subclass, it can be used in the JavaFX scene graph.

How many parents does a JavaFX scene have?

Every Scene needs exactly one Parent as the root .

Can you add multiple panes to a scene in JavaFX?

The Scene it self can only have one root Pane. So if you want 2 panes in the Scene you need 3. In your code this can look like this: StackPane rootPane = new StackPane(); Scene scene = new Scene(rootPane,...); Pane pane1 = new Pane(); Pane pane2 = new Pane(); rootPane.


1 Answers

Thanks for all the comments. Many apologies if my wording was confusing. I did add some images to show the difference but they seem to have disappeared. Anyway my thanks to @Matt for their very succinct description (which I wish I'd have thought of!). I like the mouse click idea too.

I was probably being very naughty asking this "question" on SO when I firmly believe this is a bug in javafx. Instantiating an object that goes no where near (yet) the scene graph should have no effect on it in my opinion - CSS or no CSS. I will raise a bug against javafx.

However I knew the power of SO would help me and it has! Thanks to the clue by @jewelsea I simply replaced my scene.setFill() (which was only a test anyway) with CSS and the problem is circumvented. I can even add the control to the scene now and it works as expected. From the JavaFX CSS Reference:

"The Scene object has no settable CSS properties, nor does it have any pseudo-classes. However, the root node of the scene is assigned the style class "root" (in addition to style classes already assigned to the node). "

So I set up in my css:

.root {
    -fx-background-color: blue;
}

Another way to circumvent this is to simply set the background of the StackPane to BLUE:

sp.setBackground(new Background(new BackgroundFill(Color.BLUE, null, null)));
like image 57
Andrew Waters Avatar answered Oct 18 '22 21:10

Andrew Waters