繁体   English   中英

如何将图像添加到JavaFx TableView列中

[英]How to add an Image into a JavaFx TableView column

我正在尝试找到一种方法将图像添加到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");收到错误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<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. 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"));
}

过时的答案

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

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

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

如果使用以下语句,则可以删除当前错误

imageview.setImage(new Image("arrow.png"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM