-2

This is a simple code to display image using JavaFX. However, my image can't load at all, even though I have run "Build and Clean" code.

 public void start(Stage primaryStage){
    Image image = new Image("file: images/knives.jpg");
    System.out.println("Is loaded: " + image.isError());

    //Add image to pane
    HBox pane = new HBox(10);
    pane.getChildren().add(new ImageView(image));

    //Add pane to scene
    Scene scene = new Scene(pane);

    //Add things to stage
    primaryStage.setTitle("Test");
    primaryStage.setScene(scene);
    primaryStage.show();
}
cuongtn
  • 101
  • 2
  • 8

3 Answers3

0
package imageloaded;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class ImageLoaded extends Application {

    @Override
    public void start(Stage primaryStage) {
        Image image = new Image("/images/Javafx_logo_color.png");
        System.out.println("Is loaded: " + image.isError());

        //Add image to pane
        HBox pane = new HBox(10);
        pane.getChildren().add(new ImageView(image));

        //Add pane to scene
        Scene scene = new Scene(pane);

        //Add things to stage
        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
//       
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Create images folder in under src. like

reegan@toad:~/NetBeansProjects/ImageLoaded/src$ ls
imageloaded  images
reegan@toad:~/NetBeansProjects/ImageLoaded/src/imageloaded$ ls
ImageLoaded.java
reegan@toad:~/NetBeansProjects/ImageLoaded/src/images$ ls
Javafx_logo_color.png

Other than you have used in file:

Image image = new Image("file:///home/reegan/NetBeansProjects/ImageLoaded/src/images/Javafx_logo_color.png");
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
  • Thanks for your reply. However, it didn't seem to work for me, w/wo putting the image folder under src. I do have another application that work just fine with images folder not under src. – cuongtn Oct 29 '14 at 05:53
0

Try this:

  1. use System.getPropery("user.dir") to get the application location

  2. append the location of your image file eg. image.gif . Suppose image.gif is inside src/sample/images which are packages, then following code should work.

    String appMain = System.getProperty("user.dir");

    System.out.println(appMain);

    Image image = new Image("file:"+appMain+"/src/sample/images/image.gif");

    ImageView imageView = new ImageView(image);

Kushal
  • 98
  • 12
0

In my particular case, the images were there -- in the SceneBuilder (I was using NetBeans 8.2), but the application when run did not show them. Solution in my case? In the SceneBuilder, toggle "Switch to absolute paths" next to the image's location.

Ilonpilaaja
  • 1,169
  • 2
  • 15
  • 26