Vineeth Prabhakaran
Vineeth Prabhakaran

Reputation: 641

How do I add a Scene to a stack pane in javaFX

I want to create a scene in a stackpane, so i just used this piece of code

StackPane vb = new StackPane();
Scene scene  = new Scene(vb);
vb.getParent().add(scene);

but it shows an error like The method add(Scene) is undefined for the type Parent.

Can anyone suggest me an idea for adding a scene in stackpane?

Upvotes: 1

Views: 2709

Answers (2)

FatFrank
FatFrank

Reputation: 83

A better solution that I found: add you root to the StackPane and then add it to the scene.

Like yourself, I was trying to add a number of Scenes on top of each other. The problem with what you are doing is that Scene cannot be added to anything.

So instead:

    // Create your StackPane to put everything in
    StackPane stackPane = new StackPane();

    // Create your content
    // ... Ellipse
    Ellipse ellipse = new Ellipse(110, 70);
    ellipse.setFill(Color.LIGHTBLUE);

    // ... Text
    Text text = new Text("My Shapes");
    text.setFont(new Font("Arial Bold", 24));

    // ... GridPane
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

Then add them all together inside the StackPane and add the StackPane to your Scene.

    // add all
    // now all my content appears on top of each other in my stackPane
    // Note: The order they stack is root -> ellipse -> text
    stackPane.getChildren().addAll(root, ellipse, text); 

    // make your Scene
    Scene scene = new Scene(stackPane, 300, 275, Color.BLACK);

    // finalize your stage
    primaryStage.setScene(scene);
    primaryStage.show();

Upvotes: 1

Puce
Puce

Reputation: 38132

The method add(Scene) is undefined for the type Parent.

There is not add method with any kind of parameter in Parent. Not sure what you mean here.

From the Javadoc of the Scene class:

The JavaFX Scene class is the container for all content in a scene graph.

Scene doesn't extend Node, so you cannot add to other Nodes. It's intended to be the light-weight part of a Stage.

For pagination: You could:

  • Change the scene of the stage
  • Change the root node of the scene
  • Change the content of a pane (e.g. the center of a BorderPane)

I guess for pagination the 3rd option would fit best, as you probably would want to keep the header (menu, toolbars,...) and the footer (status bar,...) when paging.

Upvotes: 1

Related Questions