Search code examples
javajavafxstackpanel

I am getting errors in my code and i don't know how to fix(Java fx stackPane)


So here is my code It is a very simple project , yet i think some modules are not able to load correctly for whatever reason, and that's why i am seeking help from someone that knows a bit around javafx and java IDEs in general. I am just trying to print a stackPane with one image and different shapes in it with labeled text near them. This code should be working just fine and i tried everything i found but nothing worked and i think it's a special case. I have tried changing the run configurations , changing the sdk , played around the project structure as much as possible but nothing removed this error

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Ellipse;
public class LabelWithGraphic extends Application {
   @Override
   public void start(Stage primaryStage) {
      ImageView leb = new ImageView(new Image("lebanon.jpg"));
      Label lb1 = new Label("Lebanon\n Our beloved country", leb);
      lb1.setStyle("-fx-border-color: red; -fx-border-width: 4");
      lb1.setContentDisplay(ContentDisplay.TOP);
      lb1.setTextFill(Color.LIMEGREEN);
      Label lb2 = new Label("Circle", new Circle(50, 50, 25));
      lb2.setContentDisplay(ContentDisplay.BOTTOM);
      lb2.setTextFill(Color.MAGENTA);
      Label lb3 = new Label("Rectangle", new Rectangle(10, 10, 50, 25));
      lb3.setContentDisplay(ContentDisplay.RIGHT);
      Label lb4 = new Label("Ellipse", new Ellipse(50, 50, 50, 25));
      lb4.setContentDisplay(ContentDisplay.LEFT);
      Ellipse ellipse = new Ellipse(50, 50, 50, 25);
      ellipse.setStroke(Color.AQUAMARINE);
      ellipse.setFill(Color.WHITE);
      StackPane stackPane = new StackPane();
      stackPane.getChildren().addAll(ellipse, new Label("JavaFx"));
      Label lb5 = new Label("A pane inside a label", stackPane);
      lb5.setContentDisplay(ContentDisplay.BOTTOM);
      HBox pane = new HBox(20);
      pane.getChildren().addAll(leb, lb2, lb3, lb4, lb5);

      Scene scene = new Scene(pane, 450, 150);
      primaryStage.setTitle("Hi guys :D");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}

And this is the error i am getting :

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
    at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:617)
    at LabelWithGraphic.start(LabelWithGraphic.java:19)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
    ... 11 more

I am still relatively new to java fx and i have no clue what i'm doing wrong. I tried many solutions but none is fixing it , I am also using IntelliJ IDE , and javafx wasn't built into it so i had to download it and add it manually. Any help would be appreciated


Solution

  • Keep image file in resource folder and use following code. I tried and it worked fine.

    InputStream is = getClass().getClassLoader().getResourceAsStream("lebanon.jpg");
    ImageView leb = new ImageView(new Image(is));