JavaFX: FXML opening image malformed URL exception protocol: e

huangapple go评论77阅读模式
英文:

JavaFX: FXML opening image malformed URL exception protocol: e

问题

我正尝试学习JavaFX,并制作一个能够从用户TableView显示用户图像的用户列表。以下是我拥有的代码部分:

@FXML private ImageView image;
@FXML
public void buttonSHow(ActionEvent actionEvent) throws MalformedURLException, FileNotFoundException {

    CList = TAbleview.getSelectionModel().getSelectedItems();
    System.out.println(CList.get(0).getPicture());
    //URL url = new URL(CList.get(0).getPicture());
    //FileInputStream input = new FileInputStream (CList.get(0).getPicture());
    image.setImage(new Image(CList.get(0).getPicture()));

}

我尝试使用这段代码的目的是使用用户列表中存储的绝对路径来设置ImageView图像,并使用Image来完成。FXML部分如下所示:

<ImageView fx:id="image"/>

当我尝试运行代码时,它会报错,错误信息是MalformedURLException: unknown protocol e。我尝试手动将URL直接输入到FXML中以查看是否可以手动加载:

<ImageView>
    <image>
        <Image url="@E:\test.jpg"/>
    </image>
</ImageView>

但仍然遇到相同的错误。

英文:

I'm trying to learn JavaFX and making a user list that can display user image from a user tableview. Here's the code that I have:

@FXML private ImageView image;
@FXML
     public void buttonSHow(ActionEvent actionEvent) throws MalformedURLException, FileNotFoundException {
      

        CList  =TAbleview.getSelectionModel().getSelectedItems();
        System.out.println(CList.get(0).getPicture());
        //URL url = new URL(CList.get(0).getPicture());
        //FileInputStream input = new FileInputStream (CList.get(0).getPicture());
        image.setImage(new Image(CList.get(0).getPicture()));
        
    }

What I'm trying to do with this code is setting up the ImageView image with an Image using the absolute path stored in the user list. The fxml has this:

&lt;ImageView fx:id = &quot;image&quot;/&gt;

When I tried to run it, it gives me the error with MalformedURLException: unknown protocol e. I tried to just punch in the url manually into the fxml to see if it loads manually,

&lt;ImageView&gt;
&lt;image&gt;
&lt;Image url = &quot;@E:\test.jpg&quot;/&gt;
&lt;/image&gt;
&lt;/ImageView&gt;

but it still gives me the same error.

答案1

得分: 1

如果您打算基于文件系统中的文件(例如用户选择的文件)来设置图像,则需要创建表示该文件的URL。

您不应该尝试自己构建此URL,而应该使用File.toURI()方法,该方法可以处理创建协议并正确解析文件路径,这些文件路径不是有效的URL(例如包含非法URL字符的文件名,比如空格)。

因此您应该这样做:

File file = new File(CList.get(0).getPicture());
image.setImage(new Image(file.toURI().toString()));
英文:

If you are intending to set the image based on a file in the file system (e.g. one chosen by the user), you need to create a URL representing the file.

You should not try to construct this yourself, but should use the File.toURI() method, which handles creating the protocol and resolving file paths that are not valid URLs (e.g. file names containing illegal URL characters, such as whitespace) correctly.

So you should do

File file = new File(CList.get(0).getPicture());
image.setImage(new Image(file.toURI().toString()));

huangapple
  • 本文由 发表于 2020年8月26日 19:47:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63596975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定