如何将图像添加到JavaFx TableView列

如何将图像添加到JavaFx TableView列,第1张

如何将图像添加到JavaFx TableView列 更新资料

由于该链接不起作用,并且我有多个请求对其进行更新,因此我用一个新的答案对其进行了更新。

从问题上来说,我不确定OP是否要从某个字段中加载图像,

NewBeautifulKiwi
或者他是否希望对所有数据显示相同的图像。因此,我将为这两种方法回答。

注意: 方法1对我而言意义不大,仅因为我对OP的要求感到困惑而存在。
问题是大约4岁,我认为从OP中进行澄清不会产生任何影响。建议的解决此类问题的方法是2。


  1. arrow.png
    为所有行加载相同的

码:

KiwiId.setCellFactory(new Callback<TableColumn<NewBeautifulKiwi, Integer>, TableCell<NewBeautifulKiwi, Integer>>() {    @Override    public TableCell<NewBeautifulKiwi, Integer> call(TableColumn<NewBeautifulKiwi, Integer> param) {      ...      TableCell<NewBeautifulKiwi, Integer> cell = new TableCell<NewBeautifulKiwi, Integer>() {         public void updateItem(Integer item, boolean empty) { if (item != null) {   imageview.setImage(new Image("arrow.png")); }         }      };      // Attach the imageview to the cell      cell.setGraphic(imageview);      return cell;    } }); KiwiId.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, Integer>("KiwiId"));

  1. 从中的a属性加载图片
    NewBeautifulKiwi
    。由于该列
    TableColumn<NewBeautifulKiwi, Image> KiwiId;
    似乎应该将自身绑定到缺少的属性图像
    NewBeautifulKiwi
    。我将介绍它,并稍后使用它绑定到此列的(重命名为“ kiwiImageCol”)单元格值工厂。

码:

@Entity(name = "KIWI_TABLE")public class NewBeautifulKiwi implements Serializable {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private int KiwiId;    private String Kiwi;    private Image kiwiImage;    public int getKiwiId() {        return KiwiId;    }    public void setKiwiId(int KiwiId) {        this.KiwiId = KiwiId;    }    public String getKiwi() {        return Kiwi;    }    public void setKiwi(String Kiwi) {        this.Kiwi = Kiwi;    }    public Image getKiwiImage() {        return kiwiImage;    }    public void setKiwiImage(Image kiwiImage) {        this.kiwiImage = kiwiImage;    }}

在表中,我将TableColumn绑定到此新属性

...@FXMLprivate TableColumn<NewBeautifulKiwi, Image> KiwiImageCol;...@Overridepublic void initialize(URL url, ResourceBundle rb) {    KiwiImageCol.setCellFactory(param -> {       //Set up the ImageView       final ImageView imageview = new ImageView();       imageview.setFitHeight(50);       imageview.setFitWidth(50);       //Set up the Table       TableCell<NewBeautifulKiwi, Image> cell = new TableCell<NewBeautifulKiwi, Image>() {public void updateItem(Image item, boolean empty) {  if (item != null) {       imageview.setImage(item);  }}        };        // Attach the imageview to the cell        cell.setGraphic(imageview);        return cell;   });   KiwiImageCol.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, Image>("kiwiImage"));}

过时的答案

您忘记将KiwiId与绑定

CellValueFactory
。请通过下面的示例,该示例在列中使用VBox,因为它需要图像和标签。您可以直接使用ImageView

https://blogs.oracle.com/javajungle/entry/how_to_pretty_up_your

一切都描述得很好。如果您仍有疑问,请随时发表评论!



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5168441.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存