Reputation: 11
i have a problem with javafx
' ImageView
.
I try to load a simple jpg
, as an image and add it to an ImageView
.
But Javafx
can't finde the resouce.
Image image = new Image("image.jpg");
ImageView iv1_1 = new ImageView();
iv1_1.setImage(image);
the exception i get is this one:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
it is crashing in the line, where the image is created.
Here is a picture of the Project: enter link description here (I am not allowed to post pictures.)
Upvotes: 0
Views: 7310
Reputation: 338
this worked for me
Image image = null;
try {
image = new Image(new FileInputStream("image.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ImageView view = new ImageView();
view.setImage(image);
Upvotes: 0
Reputation: 1349
Please find below example to load image using JavaFX.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LoadImage extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Load Image");
StackPane sp = new StackPane();
Image img = new Image("javafx.jpg");
ImageView imgView = new ImageView(img);
sp.getChildren().add(imgView);
//Adding HBox to the scene
Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Create one source folder with name Image in your project and add your image to that folder otherwise you can directly load image from external URL like following.
Image img = new Image(
"http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg"
);
Upvotes: 0