【问题标题】:How to add an Image into a JavaFx TableView column如何将图像添加到 JavaFx TableView 列中
【发布时间】:2014-03-27 03:21:34
【问题描述】:

我正在尝试找到一种方法将图像添加到 JavaFx TableView 列,该列具有通过休眠从 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;
    }
}

提前谢谢大家。

【问题讨论】:

标签: java hibernate javafx tableview


【解决方案1】:

更新

由于链接无效,并且我有多个更新请求,我正在使用新答案更新此链接。

从问题中,我不确定 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&lt;NewBeautifulKiwi, Image&gt; 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

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

【讨论】:

  • 我非常感谢这个博客示例。它让我几乎到达了我需要的地方,但我想看看你的模型作为例子。我正在尝试添加一个 tableColumn 并且我无法弄清楚如何在 colorLegend 类中添加 Circle“属性”。谢谢!
  • 没关系,我找到了。它需要是一个 ObjectProperty。但我真的很感谢这个不错的博客,它帮了很多忙。
  • 为什么 Oracle 发布代码图片而不是可复制代码?他们有什么问题?
  • @BrianStallter 如果你还记得的话,你能发布解决这个问题的方法吗?
  • 好吧,没有 circle 属性,但是为了制作单元工厂,您需要在模型中具有与您正在创建的列相对应的属性。我正在研究如何获得“圆形属性”,没有圆形属性,但对象属性确实存在,所以我使用了它。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2011-11-05
    • 2015-12-13
    • 2018-07-13
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多