Search code examples
user-interfacemethodsjavafxfxmlscenebuilder

Using JavaFX- Cannot addAll(Text field, Text area, String). Shouldn't I be able to?


My code:

public class Main extends Application {

TextArea area = new TextArea();
TextField field = new TextField();
String text = "";
public void start(Stage primaryStage){

    VBox pane = new VBox();
    Button next = new Button("Next");
    next.setOnAction(e->{
        text+= "\n" + field.getText();
        area.setText(text);
    });
    pane.getChildren().addAll(area,field,next);
    Scene scene = new Scene(pane, 700, 300);
    primaryStage.setTitle("CosmicWimpout");
    primaryStage.setScene(scene);
    primaryStage.show();

}

The error is on the .addAll, the error reads:

The method addAll(int, Collection<? extends Node>) in the type List<Node> is not applicable for the arguments (TextArea, TextField, String).

So I just edited my post to include .addAll(area, field, next). These are all GUI Nodes, yet the.addAll method does not accept these parameters.


Solution

  • As @sillyfly already pointed out the pane is of type VBox which is a subtype of Parent. The method getChildren will return an ObservableList of type Node. Therefore the method addAll on the children list will take as argument a var-arg of type Node. A String is clearly not of type Node.