将图像插入TableView JavaFX-未显示图像
我目前正在尝试将解码视频中的一些图像添加到TableView行中,但它们没有出现。只有空表列。TableView是在JavaFx场景生成器中与标签一起设计的 以下是我目前得到的信息:将图像插入TableView JavaFX-未显示图像,java,javafx,javafx-2,tableview,Java,Javafx,Javafx 2,Tableview,我目前正在尝试将解码视频中的一些图像添加到TableView行中,但它们没有出现。只有空表列。TableView是在JavaFx场景生成器中与标签一起设计的 以下是我目前得到的信息: public class MainScreenController implements Initializable { @FXML private Label previewBoxLabel; @FXML private TableView tableView; private ObservableList
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是如何工作的,如果我们能够弄清这个问题,这将是一个真正的突破
感谢您的阅读和任何帮助提前 设置CellFactory时,需要考虑到它将覆盖一些默认值,例如设置文本和图像
比如说。我必须创建双击启动的应用程序的列表视图。我必须设置一个CellFactory,以便为每个单元格的鼠标单击添加一个侦听器
applications.setCellFactory(new Callback<TreeView<Application>, TreeCell<Application>>() {
@Override
public TreeCell<Application> call(TreeView<Application> param) {
return new TreeCell<Application>() {
@Override
protected void updateItem(Application item, boolean empty) {
//call the origional update first
super.updateItem(item, empty);
//the root item in my list is null, this check is required to keep a null pointer from happening
if (item != null) {
// text and graphic are stored in the Application object and set.
this.setText(item.getApplicationListName());
this.setGraphic(item.getGraphic());
// registers the mouse event to the cell.
this.setOnMouseClicked((MouseEvent e) -> {
if (e.getClickCount() == 2) {
try {
this.getItem().launch(tabBar);
} catch (UnsupportedOperationException ex) {
Dialogs.create().nativeTitleBar().masthead("Comming Soon™").message("Application is still in development and will be available Soon™").nativeTitleBar().title("Unavailable").showInformation();
}
} else {
e.consume();
}
});
}else if(empty){
this.setText(null);
this.setGraphic(null);
this.setOnMouseClicked(null);
}
}
};
}
});
这是从其他代码拼凑而成的,所以如果您还想解释什么,请告诉我 在你们的帮助下,我设法解决了这个问题。基本上,我做的是用一堆setter和getter以及一个构造函数创建一个类,该构造函数接收ImageView并通过它的构造函数将其设置为类中的变量。然后我回到我的代码并添加了以下内容: 使用getter和setter初始化:
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的代码:
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);
请看一下教程。从一个简单的例子开始,但例12-4将让你了解细胞的工作原理。然后,也许可以通读教程。就可以了!我理解你的代码,但我遇到的问题实际上是将内容添加到单元格中。我是否需要创建一个包含getter和setter的类,并将其引用到表、列表和单元格?您需要将数据与其表示分离。如果TableView的数据类型在您的案例中是某种类型的节点ImageView,那么这几乎总是一个错误。首先找出每行中显示的数据类型,然后从相应的行数据中找出每列将显示的数据。那是你的工厂。然后找出如何在单元格中向用户实际显示这些数据。那是你的牢房工厂。@James_D是对的。我想说,如果出于某种原因您不想在表中显示节点,那么最好的做法是使用getter和setter创建某种包装类,而不是直接将其设置为该节点类型。此外,CellValueFactory和CellFactory是两种完全不同的动物,具体取决于你想做什么。CellFactory允许您超越单元格自身的默认边界。CellValueFactory允许您定义组成表格的类的特定属性,以便在该单元格中显示。细胞工厂可以让你改变细胞的行为。@LinkXXI非常感谢,伙计们:我设法在一天结束时插入了数据。你们知道如何将数据数组添加到一行而不是一列吗?