Cannot load image in JavaFX
Simply replace this code:
Image img = new Image("logo.png");
with this
Image img = new Image("file:logo.png");
Docu reference.
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
When you pass a String
to the Image
class it can be handled in four different ways (copied from docu):
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png");
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png");
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png");
// The image is located in the current working directory
Image image4 = new Image("file:flower.png");
The file:
prefix is simply an URI scheme, or in other words the counterpart to the http:
protocol classifier. This also works in the file browser, or in the web browser... ;)
For further reference, you can take a look at the wiki page of the file URI scheme: https://en.wikipedia.org/wiki/File_URI_scheme
Happy Coding,
Kalasch
Javafx can't load new added image
This works for me.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class FxTemp extends Application {
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Image image = new Image("file:///home/username/Desktop/test.png");
primaryStage.setTitle("animated");
ImageView imageView = new ImageView(image);
imageView.addEventHandler(MouseEvent.MOUSE_CLICKED, evt->{
System.out.println("click");
Image imager = new Image("file:///home/username/Desktop/test.png");
imageView.setImage(imager);
});
StackPane root = new StackPane();
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 512, 512));
primaryStage.show();
}
}
If I change the test.png and click on the view, I see the new image.
You're not really supposed to be added files to your 'resources', those are files you provide before execution. Intellij will copy the files to a location and use that location on your class path. Then you can access them via getResource
.
If you're going to have a folder that you add files to, then you want to access those files, you can do something like.
String nextImage = new File("./res/image.png").toURI().toString();
Then you should be able to load your new image provided your path is correct.
Related Topics
How to Programmatically Inject a Java Cdi Managed Bean into a Local Variable in a (Static) Method
404 Error Redirect in Spring with Java Config
What Does a Jvm Have to Do When Calling a Native Method
Spring Rest Service: How to Configure to Remove Null Objects in JSON Response
What Is the Time Complexity Performance of Hashset.Contains() in Java
Enable Partial Compilation in Intellij Idea
Spring Boot Actuator Application Won't Start on Ubuntu Vps
Java Native Method Source Code
Java How Expensive Is a Method Call
Java: Set Timeout on a Certain Block of Code
Are Thread.Sleep(0) and Thread.Yield() Statements Equivalent
How to Run MySQL In-Memory for Junit Test Cases