4

Basically, I just want to insert an image into a cell within a gridpane.

GridPane gridpane = new GridPane();
gridpane.add(new Image("File:image/myfile.jpg"));
getChildren().addAll(gridpane);

Always tells me "Image is abstract, cannot be instantiated". Which I've Googled pretty extensively vaguely found that I have to use this as a BufferedImage or something? Not actually getting it though. What am I doing wrong here?

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36

5 Answers5

14

It seems that you have the wrong import for Image (you probably have java.awt.Image). The import you need for a JavaFX image is

import javafx.scene.image.Image ;

You then need to wrap the image in an ImageView, and add the ImageView to the grid pane:

GridPane gridpane = new GridPane();
Image image = new Image("File:image/myfile.jpg")
gridpane.getChildren().add(new ImageView(image));
James_D
  • 201,275
  • 16
  • 291
  • 322
4

Try this code ;)

image = new Image("File:image/myfile.jpg");
pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
gridpane.add(pic);

Original Question

Community
  • 1
  • 1
Maverick283
  • 1,284
  • 3
  • 16
  • 33
1

You can try this.

GridPane pane = new GridPane();
FileInputStream imageStream = new FileInputStream("us.gif");
Image image = new Image(imageStream);
pane.add(new ImageView(image), 0, 0);
Ramzan
  • 11
  • 1
0

Image is an abstract class and thus cannot be instancied. You should use one of the class who extend Image, like BufferedImage

BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • 4
    This is a JavaFX question; this answer would be appropriate if the OP were asking about Swing/AWT images. – James_D Apr 15 '16 at 16:45
0

use this:

ImageView imageView = new ImageView(new Image(getClass().getResourceAsStream("picture.jpg")));