ImageView in javaFx is not shown when i run it as webstart or in browser

一世执手 提交于 2019-12-24 20:55:20

问题


I've been trying to load an image into an ImageView, and it works fine when I run it as a standalone app, but when I try to run it in the browser or as webstart it doesn't get shown.

I've tried:

ivFoto = new ImageView(new Image("file:/C:/Users/Carlos/Desktop/4fbzW.jpg"));

or

ivFoto = new ImageView(new Image("file:\\C:\\Users\\Carlos\\Desktop\\4fbzW.jpg"));

or

ivFoto = new ImageView("file:///C:/Users/Carlos/Desktop/4fbzW.jpg");

If anyone has any idea of what I'm doing wrong here, I would really appreciate the help!


回答1:


Package your image (4fbzW.jpg) in your application jar file (in the same location as the class loading the image) and reference it as a resource:

ivFoto = new ImageView(
  new Image(
    this.getClass().getResource("4fbzW.jpg").toExternalForm()
  )
);

There are a couple of things wrong with the image references in your question:

  1. Hardcoding the file: protocol.

    A JavaFX application will usually be packaged as a jar file, so resources for the application should be accessed using the jar protocol. By using the getResource method, the resource will be fetched by the same protocol used to load the class referencing the resource, thus it will automatically use the correct protocol to find the resource and you don't need to worry about this detail.

  2. Referring to local resources on your development machine.

    Webstart and browser embedded application deployment scenarios are primarily used to reference applications which are delivered over a network to a remote client. The user of the client machine could have any username or be using any supported OS, so they will be unlikely to have a file located at: C:/Users/Carlos/Desktop. By packaging the image with your application and getting it from a location relative to your application's code, then the image can always be found consistently no matter where it is deployed.

  3. File based access violates the security sandbox for WebStart and browser embedded apps

    These kind of deployment modes run applications with restricted privileges to prevent malicious applications from doing bad things (like deleting all users files or sending them to a foreign intelligence service). To allow applications to run with elevated security privileges under these deployment modes, you need to sign the applications and the user needs to explicitly trust the software provided by you. In the particular case of your app, I would not recommend signing, but instead package the resources you require with the app so that you and your users do not need to undergo the inconvenience of a signed app.


See also: Where does javafx.scene.image.Image("flower.png") look for flower.png?



来源:https://stackoverflow.com/questions/15889096/imageview-in-javafx-is-not-shown-when-i-run-it-as-webstart-or-in-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!