Reputation: 23
I need to write a program to show a picture in javafx scene, and I used ImageView to show it. But I encountered a problem.
Exception in thread "main" java.lang.IllegalArgumentException: Invalid URL: unknown protocol: f Caused by: java.net.MalformedURLException: unknown protocol: f
This is my code:
public void initialize(){
label1.setText("success");
imageView1.setImage(new Image("F:/a.jpg"));
}
Upvotes: 0
Views: 3806
Reputation: 205
Try anyone code of block. I hope it would resolve your error.
final imageView1 imv = new imageView1();
final Image image2 = new Image(Main.class.getResourceAsStream("a.jpg"));
imv.setImage(image2);
or
@FXML
private ImageView1 imageView;
@Override
public void initialize(URL location, ResourceBundle resources) {
File file = new File("F:/a.jpg");
Image image = new Image(file.toURI().toString());
imageView.setImage(image);
}
Or
FileInputStream input = new FileInputStream("F:/a.jpg");
Image image = new Image(input);
ImageView1 imageView = new ImageView1(image);
Upvotes: 1
Reputation: 311018
A filename is not a URL. A URL is a URL. This URL should read "file:/F:/a.jpg"
.
Upvotes: 1