我有一些带有父对象的 DAO 对象 - BaseDAO。我在项目的 2 个模块之间共享它 - 客户端和服务器。在某个时候,我碰到了 JFoenix 并决定使用,但后来我遇到了一个问题。要使用 JFXTreeTableView,您需要创建一个从 RecursiveTreeItem 继承的类,但由于我已经有了 DAO 类,所以我决定为我的 DAO 创建包装器,如下所示:
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());
}
}
所以基本上我想出的想法是创建一个父类作为 BaseWrapped 扩展 RecursiveTreeObject,然后从它继承其余的 DAO 类以将数据包装到 RecursiveTreeObject 中。
问题是现在我无法编写代码来处理所有 BaseWrapped 子类。我尝试了很多不同的方法,唯一对我有用的是创建表和列:
@FXML
private JFXTreeTableView<AnnouncerWrapped> mTreeTable;
JFXTreeTableColumn<AnnouncerWrapped, Integer> idColumn;
并像这样绑定数据:
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()));
但我想让代码通用,即编写类似private JFXTreeTableView<? extends BaseWrapped> mTreeTable;
通过一段代码处理所有 BaseWrapped 子类的东西。