-1
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class DisplayingText extends Application {
    @Override
    public void start(Stage stage) {
        Text text = new Text();

        text.setFont(new Font(45));

        text.setX(50);
        text.setY(50);

        text.setText("Welcome my friend!");

        Group root = new Group();

        ObservableList list = root.getChildren();

        list.add(text);

        Scene scene = new Scene(root, 600, 300);

        stage.setTitle("Sample Application");

        stage.setScene(scene);

        stage.show();

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

The compiler says I am using something not something. Here's what the compiler says:

C:\Users\Resul\Desktop\Skills\For Java>javac -Xlint:unchecked DisplayingText.jav a DisplayingText.java:25: warning: [unchecked] unchecked call to add(E) as a membe r of the raw type List list.add(text); where E is a type-variable: E extends Object declared in interface List 1 warning C:\Users\Resul\Desktop\Skills For Java>

Since I don't know English well, I translated everything the compiler said. But still, I didn't understand what exactly the compiler wants. because it's the first time I'm facing this.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    The type of root.getChildren() is ObservableList. You used ObservableList - which is called raw generic - raw means you did not specify the type of item in the list. Raw generics exist for compatibility with java 1.4, which did not have generics at all. – Lesiak Oct 25 '22 at 21:07

1 Answers1

-1

The warning is telling you that the parameter passed to add is unknown type (not compile time checked). This would be fixed if you use the proper return type of root.getChildren() which is declared as:

 ObservableList<Node> list = root.getChildren();

After this change, any attempt to add something that isn't aNode would be spotted by the compiler.

DuncG
  • 12,137
  • 2
  • 21
  • 33
  • Hi DunkG! Thank you for answering. I checked your version, but the compiler now claims that there is an error here. ---`C:\Users\Resul\Desktop\Skills For Java>javac DisplayingText.java DisplayingText.java:23: error: ';' expected ObservableList list.getChildren(); 1 error`--- – Resul Haydarmyradov Oct 25 '22 at 21:14
  • @ResulHaydarmyradov Reload the page, I made a mistake in the declaration of `list` – DuncG Oct 25 '22 at 21:15
  • Now the Compiler says: `C:\Users\Resul\Desktop\Skills For Java javac DisplayingText.java DisplayingText.java:23: error: cannot find symbol ObservableList list = root.getChildren(); symbol: class Node location: class DisplayingText error 1` The compiler points to the word – Resul Haydarmyradov Oct 25 '22 at 21:20
  • You need to `import javafx.scene.Node;` too – DuncG Oct 25 '22 at 21:23