-2

Im working on a project for school and I'm having trouble adding a child pane to a parent pane. All the code compiles except when I get to the pane.getChildren().add(Matrix); . Im able to get the code to compile when I have all the code in main, but I really want to have main call a class and create the pane there then add it to the parent pane. Im not to worried about it looking pretty right now, just want to find a way to get it to work. If anyone could help get me going in the right direction I would really appreciate it.

The compiler gives me

Button1.java:34: error: identifier expected

pane.getChildren().add(Matrix);

Button1.java:34: error: ';' expected

pane.getChildren().add(Matrix);

 public class Button1 extends Application {
public void start(Stage primaryStage) {


Scene scene = new Scene(pane, 700, 500);
primaryStage.setTitle("3 pains 1 window "); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
 primaryStage.show(); // Display the stage
}
 public static void main(String[] args) {
Application.launch(args);
}
GridPane pane = new GridPane();
MatrixPane Matrix = new MatrixPane();
pane.getChildren().add(Matrix);

}

class MatrixPane extends Pane {

double HEIGHT = 500;
double WIDTH = 200;
private GridPane pane = new GridPane();


public MatrixPane() {
}

public void fillpane() {

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            TextField text = new TextField(Integer.toString((int)(Math.random() * 2)));
            text.setMinWidth(WIDTH / 8.0);
            text.setMaxWidth(WIDTH / 10.0);
            text.setMinHeight(HEIGHT / 8.0);
            text.setMaxHeight(HEIGHT / 10.0);
                pane.add(text, j, i);
        }
    }

}


}
wawiiii
  • 15
  • 5

2 Answers2

2

That line have to be inside of a method, I suggest that it should be inside of start

public void start(Stage primaryStage) {
   pane.getChildren().add(Matrix);
   ...
Rafael
  • 555
  • 6
  • 19
-2

You have missed to include the following section inside a method().

Suparna
  • 1,132
  • 1
  • 8
  • 20
  • 1
    You can't put that in `main`. `Application.launch` blocks until the application closes, and `main` is not executed on the FX Application Thread. – James_D Apr 02 '16 at 03:13
  • Furthermore adding those lines to the main won't make those local variables accessible to any method except the main. The values in the fields will not be influenced by those method calls. – fabian Apr 02 '16 at 06:29