0
votes

I'm trying to make a javafx program that creates a checkerboard. However, when I try to run my program it throws exceptions in this line: optionsPane.getChildren().addAll(optionsPane, n_input, grid_display, label, createButton); These are the exceptions:

Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalArgumentException: Children: cycle detected: parent = HBox@ca2959, node = HBox@ca2959 at javafx.scene.Parent$2.onProposedChange(Parent.java:445) at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234) at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103) at DD_CheckerBoard.start(DD_CheckerBoard.java:40) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1 more

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class DD_CheckerBoard extends Application {

    Scene scene1, scene2; //2 scenes created to pass from a scene to another scene when create a checkerboard is clicked

    @Override
    public void start(Stage primaryStage) throws Exception{

        primaryStage.setTitle("My Checkerboard");

        //Scene 1
        HBox optionsPane = new HBox(10);
        optionsPane.setAlignment(Pos.CENTER); //sets alignment
        TextField n_input = new TextField(); //gets n from the user
        TextField grid_display = new TextField(); //only displays n x n
        grid_display.setEditable(false);
        Label label = new Label("Enter a single number to customize grid size.");
        Button createButton = new Button("Create New Checkerboard"); //button to create the new grid for checkerboard
        createButton.setOnAction(e-> primaryStage.setScene(scene2)); //calls checkerboard to the scene
        optionsPane.getChildren().addAll(optionsPane, n_input, grid_display, label, createButton); //add components
        scene1 = new Scene(optionsPane, 300,250); //create scene 1 for optionsPane

        //Scene 2
        getCheckerBoard(n_input); //create the checkerboard using the input from the user

        //SET SCENE
            primaryStage.setScene(scene1); // Place in scene in the stage, first scene is for the option pane
            primaryStage.show(); // Display the stage;
        }

    /**
     * And this method creates the checkerboard
     * @param input n by the user
     */
    public void getCheckerBoard(TextField input) {
            GridPane checkerboardPane = new GridPane(); //create a grid pane
            int n = Integer.parseInt(input.getText()); //parse input n to int
            int count = 0; //keep count of rectangles
            double s = 70; // side of rectangle
            for (int i = 0; i < n; i++) { //create the grid and rectangles
                count++; //increment count
                for (int j = 0; j < n; j++) {
                    Rectangle r = new Rectangle(s, s, s, s);
                    if (count % 2 == 0) r.setFill(Color.BLACK); //put rectangles to assigned colors in order
                    else r.setFill(Color.WHITE);
                    checkerboardPane.add(r, j, i); //add components to checkerboard
                    count++; //increment count
                } }
            scene2 = new Scene(checkerboardPane); //Create scene 2
         }

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

1 Answers

1
votes

You are trying to add your optionsPane as child to itself:

optionsPane.getChildren().addAll(optionsPane, n_input, grid_display, label, createButton);

That causes the exception you get. To fix this simply remove the optionsPane from the children list:

optionsPane.getChildren().addAll(n_input, grid_display, label, createButton);

But you also get a NumberFormatException because your Textfield is empty by default:

java.lang.NumberFormatException: For input string: ""

So you should maybe set a default value to your TextField:

TextField n_input = new TextField("0");