如何将Image添加到JavaFx TableView列中

我正在尝试找到一种方法将图像添加到JavaFx TableView列,该列包含通过hibernate从H2数据库填充的其他列中的数据。 TableView是在JavaFx Scene Builder中设计的。

到目前为止,这是我设法组建的:

控制器类:

public class HomeController implements Initializable { @FXML private TableView KIWI_TABLE; @FXML private TableColumn KiwiId; @FXML private TableColumn Kiwi; public ObservableList data; // Initializes the controller class. @Override public void initialize(URL url, ResourceBundle rb) { KiwiId.setCellFactory(new Callback<TableColumn, TableCell>() { @Override public TableCell call(TableColumn param) { //Set up the ImageView final ImageView imageview = new ImageView(); imageview.setFitHeight(50); imageview.setFitWidth(50); //Set up the Table TableCell cell = new TableCell() { public void updateItem(NewBeautifulKiwi item, boolean empty) { if (item != null) { imageview.setImage("arrow.png"); } } }; // Attach the imageview to the cell cell.setGraphic(imageview); return cell; } }); Kiwi.setCellValueFactory(new PropertyValueFactory("Kiwi")); KIWI_TABLE.setItems(gobbledyGook()); } private ObservableList gobbledyGook() { data = FXCollections.observableArrayList(); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); List courses = session.createQuery("from KIWI_TABLE").list(); for (Iterator iterator = courses.iterator(); iterator.hasNext();) { NewBeautifulKiwi course = (NewBeautifulKiwi) iterator.next(); System.out.println(course.getKiwi()); data.add(course); } transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } return data; } } 

我在imageview.setImage("arrow.png");收到错误imageview.setImage("arrow.png");Incopatibletypes: String cannot be converted to Image

这是我第一次尝试将图像添加到TableView中。

我从昨天开始环顾四周,但现在我似乎陷入困境。 我希望得到一些帮助。 我真的很感激一些帮助。

这是创建数据库pojos的类:

 @Entity(name = "KIWI_TABLE") public class NewBeautifulKiwi implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int KiwiId; private String Kiwi; 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; } } 

谢谢大家。

更新

由于链接不起作用,我有多个请求更新它,我正在用一个新的答案更新它。

从这个问题来看,我不确定OP是否希望从NewBeautifulKiwi某个字段加载图像,或者他是否想要为所有数据显示相同的图像。 所以,我会回答这两种方法。

注意: 方法1对我没有多大意义,只是因为我对OP的要求感到困惑而存在。 问题是〜4岁,我认为OP的澄清不会产生任何影响。 处理这类问题的建议方法是2。


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

码:

 KiwiId.setCellFactory(new Callback, TableCell>() { @Override public TableCell call(TableColumn param) { ... TableCell cell = new TableCell() { 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("KiwiId")); 

  1. NewBeautifulKiwi的属性加载图像。 由于列TableColumn 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绑定到这个新属性

 ... @FXML private TableColumn KiwiImageCol; ... @Override public 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 cell = new TableCell() { 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("kiwiImage")); } 

过时的答案

你忘了用一个CellValueFactory绑定KiwiId。 请通过以下示例,该示例在列中使用VBox,因为它需要Image和标签。 您可以直接使用ImageView

http://sofzh.miximages.com/java/ppre codeimageview.setimage(new image(arrow.png”));