将图像插入TableView JavaFX - 图像未显示

时间:2014-04-03 16:48:36

标签: java javafx javafx-2 tableview

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

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

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在过去几天的工作方式,如果我们能够深入了解这将是一个真正的突破。

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

2 个答案:

答案 0 :(得分:0)

设置CellFactory时,您需要考虑它将覆盖某些默认的bevaiours,例如设置文本和图像。

例如。我必须创建一个ListView Application的双击启动。我必须设置一个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);
                    }
                }
            };
        }
    });

这是从其他一些代码拼凑而成的,所以如果您还有其他想要解释的内容,请告诉我们!

答案 1 :(得分:0)

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

使用Getters和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);