Kuba K
Kuba K

Reputation: 33

JavaFX VBox getChildren().addAll() error

I have a small problem, When i want add textField, button and label to VBOX i have a error : addAll() in ObservableList cannot be applied to:

Image

in this place: vBox.getChildren().addAll(textField, button, label);

public class Main extends Application {

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

        BorderPane layout = new BorderPane();

        Scene scene = new Scene(layout, 400, 200);

        TextField textField = new TextField();
        Label label = new Label("Average: 0.0");

        Button button = new Button("Przycisk");
        button.setOnAction(event -> {
            String textFromTextField = textField.getText();
            String[] splittedText = textFromTextField.split(",");
            double average = 0.0;
            for (String s: splittedText) {
                average += Double.parseDouble(s);
            }
            average /= splittedText.length;
            label.setText("Average: " + average);
        });

        VBox vBox = new VBox();
        vBox.getChildren().addAll(textField, button, label);
        vBox.setAlignment(Pos.CENTER);
        vBox.setSpacing(10);

        layout.setCenter(vBox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("App");
        primaryStage.show();
    }
}

Upvotes: 0

Views: 3786

Answers (2)

n247s
n247s

Reputation: 1918

Take a good look at your imports!

In the image you can see that you used the TextField and Label from the 'awt' library instead of the 'javafx' library. Make sure to change that and see if it worked out!

(Ps. Please post the full stacktrace in your question next time!)

Upvotes: 0

Pavlo Viazovskyy
Pavlo Viazovskyy

Reputation: 937

That's because you have imported AWT components java.awt.Label and java.awt.TextField instead of JavaFX components javafx.scene.control.Label and javafx.scene.control.TextField.

Upvotes: 2

Related Questions