我正在尝试找到一种方法将图像添加到JavaFx TableView列,该列包含通过hibernate从H2数据库填充的其他列中的数据。 TableView是在JavaFx Scene Builder中设计的。
到目前为止,这是我设法汇总的内容:
控制器类:
public class HomeController implements Initializable {
@FXML
private TableView<NewBeautifulKiwi> KIWI_TABLE;
@FXML
private TableColumn<NewBeautifulKiwi, Image> KiwiId;
@FXML
private TableColumn<NewBeautifulKiwi, String> Kiwi;
public ObservableList<NewBeautifulKiwi> data;
// Initializes the controller class.
@Override
public void initialize(URL url, ResourceBundle rb) {
KiwiId.setCellFactory(new Callback<TableColumn<NewBeautifulKiwi, Image>, TableCell<NewBeautifulKiwi, Image>>() {
@Override
public TableCell<NewBeautifulKiwi, Image> call(TableColumn<NewBeautifulKiwi, Image> 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(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<NewBeautifulKiwi, String>("Kiwi"));
KIWI_TABLE.setItems(gobbledyGook());
}
private ObservableList<NewBeautifulKiwi> 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");
处出现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;
}
}
提前谢谢大家。
答案 0 :(得分:5)
由于链接不起作用,我有多个请求更新它,我正在用新答案更新它。
从这个问题来看,我不确定OP是否希望从NewBeautifulKiwi
中的某个字段加载图像,或者他是否想要为所有数据显示相同的图像。所以,我会回答这两种方法。
注意: 方法1对我没有多大意义,只是因为我对OP的要求感到困惑而存在。问题是〜4岁,我认为OP的澄清不会产生任何影响。处理这类问题的建议方法是2。
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"));
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绑定到这个新属性
...
@FXML
private TableColumn<NewBeautifulKiwi, Image> 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<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,因为它需要Image和标签。您可以直接使用ImageView
https://blogs.oracle.com/javajungle/entry/how_to_pretty_up_your
一切都描述得非常好。如果您仍有疑问,请随时发表评论!
答案 1 :(得分:0)
如果您使用以下声明
,则可以删除您当前的错误imageview.setImage(new Image("arrow.png"));