1

I made a circle and added as a child to a group. then I added the group as a child to a layout(Region). I added Region to the scene. I made both with different colours but I cannot see the circle

Analog_clock.java

package analog_clock;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Analog_clock extends Application {

    @Override
    public void start(Stage primaryStage) {

        Circle circle = new Circle();
        circle.setCenterX(100.0f);
        circle.setCenterY(100.0f);
        circle.setRadius(50.0f);
        circle.setFill(Color.ALICEBLUE);

        Group g = new Group();
        g.getChildren().add(circle);

        Background_region_ bg = new Background_region_();
        bg.getChildrenUnmodifiable().add(g);

        Scene scene = new Scene(bg, 300, 250);
        scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());

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

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

}

Background_Region_.java

package analog_clock;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Background_region_ extends Region 
{
    //CONSTRUCTOR
    public Background_region_()
    {
        setStyle("-fx-background-color: #ACACE6");
    }

}

style.css

.circle{-fx-stroke: #cdd0d7;}

2 Answers2

2

The problem is the Region Class only has an Unmodifiable List of children via its public API, that means, the only way to add children to it is to subclass it (e.g Pane). So use Pane or another subclass, something like this for example:

Pane bg = new Pane();
bg.setBackground(new Background(new BackgroundFill(Color.web("#ACACE6"), null,null)));
bg.getChildren().add(g);

enter image description here

Yahya
  • 13,349
  • 6
  • 30
  • 42
  • Please have a look at this : https://stackoverflow.com/questions/44999779/javafx-my-css-file-is-not-working –  Jul 09 '17 at 21:35
1

Use Pane instead of Region. Region is a special parent class for control's developers.

Next line throws exception in your code:

bg.getChildrenUnmodifiable().add(g);

Note word "Unmodifiable". It means your are not supposed and can't modify this list.

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • Please have a look at this: https://stackoverflow.com/questions/44999779/javafx-my-css-file-is-not-working –  Jul 09 '17 at 21:34