0

I want to make a border pane with two HBoxes on top and bottom and a GridPane in the center... I wrote what I needed, attached the labels but I can't run the code Exception in Application start method java.lang.reflect.InvocationTargetException is what I get as an error... the code is below, any help is welcome :) thanks

public class labelBorder extends Application {

    @Override
    public void start(Stage primaryStage) {

        BorderPane bp = new BorderPane();
        bp.setPrefSize(400, 400);

        HBox hb1 = new HBox();
        Label lb1 = new Label("");
        lb1.setPrefWidth(200);
        lb1.setBorder(new Border(new BorderStroke(Color.AQUAMARINE,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
        Label lb2 = new Label("");
        lb2.setPrefWidth(200);
        lb2.setBorder(new Border(new BorderStroke(Color.BLUEVIOLET,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));


        HBox hb2 = new HBox();
        URI foto = Paths.get("D:\\Barca.jpg").toUri();
        Label lb3 = new Label();
        lb3.setGraphic(new ImageView(foto.toString()));
        lb3.autosize();

        GridPane gp = new GridPane();
        Label lb4 = new Label("");
        Label lb5 = new Label("");
        Label lb6 = new Label("");
        Label lb7 = new Label("");

        gp.add(lb4, 0, 0);
        gp.add(lb5, 0, 1);
        gp.add(lb6, 1, 0);
        gp.add(lb7, 1, 1);

        gp.getChildren().addAll(lb4,lb5,lb6,lb7);           
        hb1.getChildren().addAll(lb1,lb2);
        hb2.getChildren().addAll(lb3);


        bp.setTop(hb1);
        bp.setCenter(gp);
        bp.setBottom(hb2);
        bp.getChildren().addAll(hb1,hb2,gp);

        Scene scene = new Scene(bp);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Draven
  • 19
  • 1
  • 7

1 Answers1

0

You should NOT add the duplicate children controls to your parent layout(pane). Your code must be throwing,

java.lang.IllegalArgumentException: Children: duplicate children added

To overcome your issue remove these lines,

gp.getChildren().addAll(lb4,lb5,lb6,lb7);

And,

bp.getChildren().addAll(hb1,hb2,gp); 

As those controls have already been added to your corresponding layout.

Note: Adding children with pane.getChildren().addAll(...) on BorderPane is irrelevant and will be ignored while rendering the added controls.

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • Just saw it, and removed the line....still the code won`t execute... I made sure the build path is right and the module build path. Other codes run perfectly but this one would not.... the error I am getting now Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) – Draven Jan 19 '20 at 15:13
  • I have tested it and it's working - try *re-building* it and *re-run* your app. – Shekhar Rai Jan 19 '20 at 15:15
  • @Draven The information that you get an `InvocationTargetException` is practically useless. Every exeption happening in `start` is wrapped in this kind of exception. The part of the stacktrace that's actually interesting is the part starting at the last `Caused by`; this should also mention lines in your own code... – fabian Jan 19 '20 at 15:20
  • @fabian, thanks for the info, I can post what is cause by – Draven Jan 19 '20 at 15:24
  • Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) at java.base/java.lang.Thread.run(Thread.java:830) – Draven Jan 19 '20 at 15:24
  • @ShekharRai *"Adding children with `pane.getChildren().addAll`" on a `GridPane` is irrelevant and will be ignored"* That part of the sentence is wrong. Adding children to a `GridPane` can very well be done; the only downside is that you cannot specify the layout parameters in the same go, 0 will be used for indices that are not specified and 1 for spans that are not specified. You can however use static methods to set those, e.g. `GridPane.setRowIndex(child, index)`; In fact using `GridPane` from fxml is only possible because of that fact. – fabian Jan 19 '20 at 15:25
  • still no result – Draven Jan 19 '20 at 15:47
  • @Draven I don't believe you've posted the _root_ cause. See [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/6395627). – Slaw Jan 19 '20 at 18:39