Search code examples
javajavafxresolve

Cannot resolve method getChildren.addAll();


Okay, I am not sure if I am asking this in the right place but I am hoping someone here can help me. So, I am a beginner at Java and I am trying to make a JavaFX application but my Layout 1 "getChildren.addAll(label1, button1);" is being labeled as an error. That error is:

Cannot resolve method 'addAll(java.awt.Label, javafx.scene.control.Button)'

Any advice or help toward the problem is greatly appreciated. Thank You, if you read this.


package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

import java.awt.*;

  public class Main extends Application {

Stage window;
Scene scene1, scene2;

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

@Override
public void start(Stage primaryStage) {
    window = primaryStage;

    //Button 1
    Label label1 = new Label("Welcome to the first scene!");
    Button button1 = new Button("Go to scene 2");
    button1.setOnAction(e -> window.setScene(scene2));

    //Layout 1 - children laid out in vertical column
    VBox layout1 = new VBox(20);
    layout1.getChildren().addAll(label1, button1);
    scene1 = new Scene(layout1, 200, 200);

    //Button 2
    Button button2 = new Button("Back to Scene 1");
    button2.setOnAction(e -> window.setScene(scene1));

    //Layout 2
    StackPane layout2 = new StackPane();
    layout2.getChildren().add(button2);
    scene2 = new Scene(layout2, 450, 500);

    window.setScene(scene1);
    window.setTitle("The Title");
    window.show();
}


 }

Solution

  • Just make sure you don't import any classes from java.awt.*.

    In your exception, it already says you are using java.awt.Label. You need the JavaFX version:

    import javafx.scene.control.Label;