如何将图像添加到JavaFx TableView列
我正在尝试找到一种将图像添加到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中。
从昨天开始,我一直四处张望,但现在似乎被困住了。我希望能有所帮助。我真的很感谢您的帮助。
@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。
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"));
- 从中的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绑定到此新属性
...@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,因为它需要图像和标签。您可以直接使用ImageView
https://blogs.oracle.com/javajungle/entry/how_to_pretty_up_your
一切都描述得很好。如果您仍有疑问,请随时发表评论!
以上是 如何将图像添加到JavaFx TableView列 的全部内容, 来源链接: utcz.com/qa/431442.html