0

I don't know how to add image in JFXTreeTableView because when I set my variable type to ImageView I get an error that I should give it String data. This is my class and constructor:

class TeamsInnerClass extends RecursiveTreeObject<TeamsInnerClass> {
    ImageView icon;
    SimpleStringProperty teamName;
    ...

    public TeamsInnerClass(ImageView icon, String teamName) {
        this.icon = icon;
        this.teamName = new SimpleStringProperty(teamName);
    }
}

This is the controller:

    JFXTreeTableColumn<TeamsInnerClass, ImageView> icon = new JFXTreeTableColumn<>();
    icon.setPrefWidth(100);
    icon.setCellValueFactory(param -> param.getValue().getValue().icon);
    JFXTreeTableColumn<TeamsInnerClass, String> teamName = new JFXTreeTableColumn<>("Teams");
    teamName.setPrefWidth(100);
    teamName.setCellValueFactory(param -> param.getValue().getValue().teamName);
    ...

and the data that is made with constructor:

    ObservableList<TeamsInnerClass> teamsInnerClass = FXCollections.observableArrayList();
    teamsInnerClass.add(new TeamsInnerClass(new ImageView(new Image("League/backs/background2.jpg")), "Real"));
    final TreeItem<TeamsInnerClass> root = new RecursiveTreeItem<>(teamsInnerClass, RecursiveTreeObject::getChildren);
    scoresTable.getColumns().addAll(icon, teamName);
    scoresTable.setRoot(root);
    scoresTable.setShowRoot(false);

and the error that I get:

enter image description here

  • 1
    Wrap the `ImageView` in an `ObservableValue`. _Note: Model classes should not know about GUI objects._ – Slaw Dec 12 '18 at 11:45
  • 1
    Wrapping the value in a `SimpleObjectProperty` should do the trick... – fabian Dec 12 '18 at 11:53
  • Thank you @fabian it worked. I did it in class constructor. Now is there a way to set width and Height for this? – Hossein Mortazavifar Dec 12 '18 at 12:42
  • `fitWidth` and `fitHeight` properties of the `ImageView` passed to the costructor? BTW: I'd recommend changing the property to `Image` and using a custom cell implementation with a `ImageView` as `graphic` for the cell instead of adding a `ImageView` property to the item class... – fabian Dec 12 '18 at 13:26

0 Answers0