1

I have a few DAO object with a parent one - BaseDAO. I share it between 2 modules of the project - client and server. At some point I bumped into JFoenix and decided to use but then I've met a problem. To use JFXTreeTableView you need to make a class inherited from RecursiveTreeItem, but since I already have the DAO classes I decided to create wrappers for my DAO like this:

    class AnnouncerWrapped extends BaseWrapped<AnnouncerWrapped> {
    StringProperty education;

    public AnnouncerWrapped(Announcer announcer) {
        super(announcer);
        this.education = new SimpleStringProperty(announcer.getEducation());
    }

    public static ObservableList<AnnouncerWrapped> wrap(ArrayList<? extends BaseDAO> announcers) {
        List<AnnouncerWrapped> wrappedAnnouncers = new ArrayList<>();
        for (BaseDAO announcer : announcers) {
            AnnouncerWrapped wrappedAnnouncer = new AnnouncerWrapped((Announcer) announcer);
            wrappedAnnouncers.add(wrappedAnnouncer);
        }
        return FXCollections.observableArrayList(wrappedAnnouncers);
    }
}

class BaseWrapped<T> extends RecursiveTreeObject<T> {
    IntegerProperty id;
    StringProperty name;

    public BaseWrapped(BaseDAO baseDAO) {
        this.id = new SimpleIntegerProperty(baseDAO.getId());
        this.name = new SimpleStringProperty(baseDAO.getName());
    }
}

So basically the idea I came up with is to create a parent class as BaseWrapped that extends RecursiveTreeObject and then inherit the rest DAO classes from it to wrap the data into RecursiveTreeObject.

The problem is that now I can't write a code to deal with all BaseWrapped subclasses. I tried a lot of different ways and the only one working for me is to create table and column as:

@FXML
private JFXTreeTableView<AnnouncerWrapped> mTreeTable;

JFXTreeTableColumn<AnnouncerWrapped, Integer> idColumn;

And bind the data like this:

ObservableList<AnnouncerWrapped> announcers = null;
        try {
            announcers = FXCollections.observableArrayList(AnnouncerWrapped.wrap(request.getData()));
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        // build tree
        final TreeItem<AnnouncerWrapped> root = new RecursiveTreeItem<>(announcers, RecursiveTreeObject::getChildren);

        mTreeTable.setRoot(root);
        mTreeTable.setShowRoot(false);
        mTreeTable.setEditable(false);
        mTreeTable.getColumns().setAll(idColumn);

        JFXTextField filterField = new JFXTextField();
        Label size = new Label();
        size.textProperty().

                bind(Bindings.createStringBinding(() -> mTreeTable.getCurrentItemsCount() + "",
                        mTreeTable.currentItemsCountProperty()));

But I would like to make the code universal i.e. write something like private JFXTreeTableView<? extends BaseWrapped> mTreeTable; to deal with all BaseWrapped subclasses via one piece of code.

Volkeee
  • 93
  • 9

0 Answers0