0

I am having problem in adding logo to my javafx desktop application. The error received is "Invalid URL: Invalid URL or resource not found". I checked for this but there was no such mistake.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.application.Application;
import javafx.fxml.*;

public class Main extends Application{

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

    public void start(Stage primaryStage) {
        try  {
            primaryStage.setTitle("App");
            primaryStage.setIconified(true);
            primaryStage.getIcons().add(new Image("logo.jpg"));
            AnchorPane root=new AnchorPane();
            root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene s=new Scene(root, 800, 600);
            primaryStage.setScene(s);
            primaryStage.show();
        } 
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
1
  • "Sample.fxml" needs to be in the same package as the class Commented Mar 9, 2014 at 8:49

1 Answer 1

1

The class Image has one constructor that takes a String argument. This argument is the URL of the image. "logo.jpg" is not a valid URL. You would have better luck using the InputStream variant of the constructor:

primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.jpg")));

(in the case above, logo.jpg has to be in the same package as the class; modify the path accordingly if it isn't)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.