How do I add a pane inside another pane via code like javafx?

Asked

Viewed 420 times

0

I already know how to do that when it comes to a AnchorPane, is something like this:

AnchorPane ap = new AnchoPane();
Label lbl = new Label("Qualquer");
ap.getChildren().add(lbl);

But when it comes to one Pane normal, I can only call the getChildrenUnmodifiable(), and if later I call the add(lbl) the following exception shall be made:

Exception in thread "Javafx Application Thread" java.lang.Runtimeexception: java.lang.reflect.Invocationtargetexception

Can someone help me?

  • Provide a code that is reproducible. [mcve]

1 answer

0

Pane also has the method getChildren(). You can use it the same way as Anchorpane. Make sure you are using the correct import: import javafx.scene.layout.Pane;

See the class documentation at this link: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Pane.html

To facilitate I took a print of the part that matters.

inserir a descrição da imagem aqui

Code snippet for testing:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Pane pane = new Pane();

            Pane otherPane = new Pane();
            pane.getChildren().add(otherPane);

            Label label = new Label("Qualquer");
            otherPane.getChildren().add(label);


            Scene scene = new Scene(pane,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.