使用泛型类型构造 ObservableList

我正在尝试扩展 JavaFX TableView 类,以便我可以将实体类传递给它,它会显示该类的所有已保存对象。我可以让它像这样工作,但我希望我的自定义类足够通用,只接受一个 Class 变量并处理其余的:


private void showSuppliers()

{

    tabPane.getTabs().stream().filter((tab) -> (tab.getText().compareTo("Fournisseurs") == 0)).map((tab) -> (VBox)tab.getContent()).forEachOrdered((vBox) -> 

    {

        TableView<Supplier> table = new TableView<>();


        TableColumn nameColumn = new TableColumn("Nom");

        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));


        TableColumn addressColumn = new TableColumn("Addresse");

        addressColumn.setCellValueFactory(new PropertyValueFactory<>("address"));


        TableColumn contactColumn = new TableColumn("Contact");

        contactColumn.setCellValueFactory(new PropertyValueFactory<>("contactPerson"));


        TableColumn phoneNumberColumn = new TableColumn("Téléphone");

        phoneNumberColumn.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));


        TableColumn emailColumn = new TableColumn("Courriel");

        emailColumn.setCellValueFactory(new PropertyValueFactory<>("emailAddress"));


        TableColumn creationDateColumn = new TableColumn("Date de création");

        creationDateColumn.setCellValueFactory(new PropertyValueFactory<>("enteredDate"));


        table.getColumns().addAll(nameColumn, addressColumn, contactColumn, phoneNumberColumn, emailColumn, creationDateColumn);


        ObservableList<Supplier> suppliers = FXCollections.observableArrayList(supplierService.findAll());

        table.setItems(suppliers);


        vBox.getChildren().add(table);

    });

}

同样,上述方法效果很好,但我必须为每个列出我想要避免的不同数据的表重复它。


如您所见,添加列不是问题。我不明白的是如何使用我的 T 来制作这样的 ObservableList :


ObservableList<Supplier> suppliers = FXCollections.observableArrayList(supplierService.findAll());

我尝试了更换<Supplier>的<T>或<Class<T>>和似乎并不有效。当这些东西几乎不存在时,我就学会了 Java,而且我在网上找到的关于它的信息说实话有点令人困惑。


如何使用我的 T 变量作为类型构建我的列表?


拉风的咖菲猫
浏览 167回答 1
1回答

白板的微信

嗯,这很简单,我只是完全删除了它!这是最终结果,只需传递一个实体类和一个列表,其余的由表来处理!public class EntityTable<T> extends TableView<T>{&nbsp; &nbsp; private final Class<T> entityClass;&nbsp; &nbsp; private final List<T> entityList;&nbsp; &nbsp; public EntityTable(Class<T> entityClass, List<T> entityList)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.entityClass = entityClass;&nbsp; &nbsp; &nbsp; &nbsp; this.entityList = entityList;&nbsp; &nbsp; &nbsp; &nbsp; init();&nbsp; &nbsp; }&nbsp; &nbsp; private void init()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (Field field : entityClass.getDeclaredFields())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((field.getType() != Set.class) && (field.getName().compareTo("id") != 0))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableColumn column = new TableColumn(field.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; column.setCellValueFactory(new PropertyValueFactory<>(field.getName()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getColumns().add(column);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ObservableList list = FXCollections.observableArrayList(entityList);&nbsp; &nbsp; &nbsp; &nbsp; setItems(list);&nbsp; &nbsp; }}缺点是这将在类中显示带有实际属性名称的列标题(并不总是非常用户友好)。欢迎任何自动在列标题中放置更好的东西的建议!编辑对于我的列名,我最终创建了一个名为 FriendlyName 的自定义注释,只有一个值参数。我现在像这样填充列标题:FriendlyName friendlyName = field.getAnnotation(FriendlyName.class);TableColumn column = new TableColumn(friendlyName.value());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java