将图像插入到TableView JavaFX中

编程入门 行业动态 更新时间:2024-10-24 22:19:58
本文介绍了将图像插入到TableView JavaFX中-不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在尝试将一些图像从解码视频添加到TableView行中,但它们没有出现.仅空的TableColumns. TableView是在JavaFx Scene Builder中与Label一起设计的.

I'm currently trying to add some images from a decoded video to a TableView row and they are not appearing. Only empty TableColumns. The TableView has been designed in JavaFx Scene Builder along with the Label.

这是我到目前为止所得到的:

Here's what I got so far:

public class MainScreenController implements Initializable { @FXML private Label previewBoxLabel; @FXML private TableView tableView; private ObservableList<ImageView> imageList = FXCollections.observableArrayList(); @FXML public void AddClipBeta(){ //Code which uses an external class in order to decode video (Variables Frames, width and height are not shown but are present in the actual code) VideoSegment clip = new VideoSegment(0, file.getPath(), 0, Frames, width, height); //Opens the file in decoding class - ready to output frames try{clip.openFile();} catch(Exception e){} //First frame is updated on the preview box previewBoxLabel.setGraphic(new ImageView(convertToFxImage(clip.getThumbnail()))); System.out.println(file.getPath()); int i =0; //While loop in test phase to see whether or not 10 frames will be visible in the table while(i != 10){ //Creates and sets columns to tableView TableColumn<ImageView, ImageView> col = new TableColumn<ImageView, ImageView>(); col.setPrefWidth(100); //Set width of column tableView.getColumns().add(col); col.setCellFactory(new Callback<TableColumn<ImageView, ImageView>, TableCell<ImageView, ImageView>>() { @Override public TableCell<ImageView, ImageView> call(TableColumn<ImageView, ImageView> p) { TableCell<ImageView, ImageView> cell = new TableCell<ImageView, ImageView>(){ }; return cell; } }); //Adds current frame to list imageList.add(new ImageView(convertToFxImage(clip.getThumbnail()))); //Gets next video frame try{clip.getNextFrame();} catch(Exception e){} //Updates counter i++; } //Sets list of frames on the table tableView.setItems(imageList); } // There is a problem with this implementation: transparent pixels on the BufferedImage aren't converted to transparent pixels on the fxImage. public static javafx.scene.image.Image convertToFxImage(java.awt.image.BufferedImage awtImage) { if (Image.impl_isExternalFormatSupported(BufferedImage.class)) { return javafx.scene.image.Image.impl_fromExternalImage(awtImage); } else { return null; } }

我一直在努力地理解TableView在最近几天的工作方式,如果我们能够深入浅出,那将是一个真正的突破.

I've been struggling understanding how the TableView works the last couple of days and it would be a real breakthrough if we could get to the bottom of this.

感谢您的阅读和任何提前的帮助!

Thanks for reading and any help in advance!

推荐答案

我在你们的帮助下设法解决了这个问题.基本上,我所做的是创建一个带有一堆setter和getter的类以及一个接受ImageViews并通过其构造函数将其设置为类中变量的构造函数.然后,我回到我的代码并添加了以下内容:

I managed to sort this out with the help of you guys. Basically, what I did was make a class with a bunch of setters and getters and a constructor that takes in ImageViews and sets it to a variable in the class via it's constructors. Then I went back to my code and added the following:

带有Getter和Setter的类:

Class with Getters and Setters:

import javafx.scene.image.ImageView; public class tableDataModel { private ImageView image; public tableDataModel(ImageView image){ this.image = image; } public ImageView getImage(){ return image; } public void setImage(ImageView image){ this.image = image; }

}

MainScreenController中的代码:

Code from MainScreenController:

TableColumn<tableDataModel, ImageView> col = new TableColumn<>(); tableView.getColumns().add(col); imageList.add(new tableDataModel(new ImageView(convertToFxImage(clip.getThumbnail())))); col.setPrefWidth(50); col.setCellValueFactory(new PropertyValueFactory<tableDataModel, ImageView>("image")); int i = 0; while (i != 10) { try { imageList.add(new tableDataModel(new ImageView(convertToFxImage(clip.getNextFrame())))); } catch (Exception e) { } i++; } tableView.setItems(imageList);

更多推荐

将图像插入到TableView JavaFX中

本文发布于:2023-11-26 20:20:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635055.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   TableView   JavaFX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!