-2

I tried to run this code in intelij IDE, but it shows an error, java: cannot find symbol, symbol: class StackPane

But when I tried the same code on Netbeans IDE, it worked well. what could be the reason for that?

package javafxfirstproject;

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

public class JavaFxFirstProject extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}
  • 1
    Have you actually added the JavaFX library to your classpath in IntelliJ? – Zephyr May 22 '21 at 14:38
  • yes I did so. but still having a warning `java: warning: source release 11 requires target release 11`. I'm using java 1.8 version. Is that the problem? – SamarakoonSMMC May 24 '21 at 05:47
  • You didn't think to include that in your original question? Please read [ask] and take the [tour] to learn how best to use Stack Overflow. – Zephyr May 24 '21 at 11:11

1 Answers1

-2

You should follow Intellij IDEA JavaFX tutorial, especially Adding the JavaFX library and Add VM options.

Here is post with simillar issue.

Go through this YouTube tutorial, your code works on my computer after completing this tutorial.

kuch
  • 192
  • 1
  • 5
  • Thank you very much for the answer. I have added both javaFX library and VM options but still having the same issue. – SamarakoonSMMC May 22 '21 at 15:19
  • Check this [YouTube tutorial](https://www.youtube.com/watch?v=qn2tbftFjno). – kuch May 22 '21 at 15:44
  • I updated my java `version 1.8` to `version 11` and then followed the tutorial. And now it is working. Thank you very much for your support. – SamarakoonSMMC May 24 '21 at 08:31