Loading an image in ImageView through code

后端 未结 1 455
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 16:23

I have built my application using scenebuilder for javafx. I have a form where a person has to upload an image. I used this code

public void photoChooser(Act         


        
相关标签:
1条回答
  • 2020-12-21 16:36

    The constructor of Image expects an URL and not a file path. Therefore if there is a ":" in the string, everything up to that point is interpreted as the protocol (normally something like http, file or ftp).

    You have to change the line

    String img = file.toString();
    

    to

    String img = file.toURI().toURL().toExternalForm();
    

    This gets the URL from the file before converting to string. I converted to URI first since File.toURL is deprecated and that's the suggested "workaround".

    0 讨论(0)
提交回复
热议问题