Inserting images into TableView rows - JavaFX

谁说胖子不能爱 提交于 2019-12-25 01:55:32

问题


Just wondering how to insert an image into cells of a TableView in JavaFX.

I have read other peoples solutions on this site but struggle a tad understanding them as I think their set up is a bit different to mine.

Pretty much I am making a game library, and in the tableview I want an icon of the game. The data is being read from a .dat file (in which this case a String of the icon's URL), and all the strings in the rows work.

The column is made

@FXML
private TableColumn<Game, Image> iconCol;

In initialize() the cells are being set.

iconCol.setCellValueFactory(cellData -> cellData.getValue().iconURLProperty());

I get an error here, as iconURLProperty is the string of the URL of the icon image I want set.

I know I need an imageview somewhere and I know I need to assign the url to it, but I'm not sure how.

Please let me know if you need further code, and again, I have checked other similar issues on the site with no real solution. Thanks for the help :)

/UPDATE\ Hi guys, thanks for the response. I have given some of the questions you's mentioned a more thorough look and have tried implementing them into mine.

I am now getting the same error in this question: How to add an Image into a JavaFx TableView column

However I have tried applying the solution mentioned of including the CellValueFactory but the problem still remains. Here is what I have, the column being made(in scenebuilder):

@FXML
private TableColumn<Game, Image> iconCol;

Game is the class that holds each game's information.

Then in initialize I have this for the iconCol:

iconCol.setCellValueFactory(new PropertyValueFactory<>("icon"));
    iconCol.setCellFactory(new Callback<TableColumn<Game, Image>, TableCell<Game, Image>>() {

        @Override
        public TableCell<Game, Image> call(TableColumn<Game, Image> param) {
            final ImageView imageview = new ImageView();
            imageview.setFitHeight(50);
            imageview.setFitWidth(50);

            //Set up the Table
            TableCell<Game, Image> cell = new TableCell<Game, Image>() {
                public void updateItem(Game game, boolean empty) {
                    //     if (item != null) {
                    imageview.setImage(game.iconURLProperty());
                    //   }
                }
            };

            // Attach the imageview to the cell
            cell.setGraphic(imageview);
            return cell;
        }
    });

I'm getting an error on this line:

imageview.setImage(game.iconURLProperty());

As it says StringProperty cannot be converted to Image. iconURLProperty() retrieves the URL (eg."icon/fallout.jpg") of the image I want displayed.

Apologies for any dumb moments too, I've only just started JavaFX about a month ago. Thank you all for your help in advance :)

/UPDATE 2\ I have solved this problem, however it still isn't working, any ideas? :/

来源:https://stackoverflow.com/questions/25601037/inserting-images-into-tableview-rows-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!