TableView用于整个应用程序中的不同对象

时间:2017-07-10 12:00:08

标签: java javafx tableview javafx-8

是否可以使TableView能够在运行时维护不同类型的对象,但一次只能使用一种对象?

我面临的具体情况是我想用来显示搜索结果的TableView。这些搜索可能会产生不同的“对象”作为结果,假设强制性PersonAddress。当用户搜索其中任何一个时,结果将是PersonAddress的集合 搜索后,表格会相应更改(getItems().clear();getColumns().clear();)但是我已经停止执行填充。
我将使用FXML初始化的TableView声明为TableView<?> searchResults。现在我尝试在表中添加new TableColumn<Person, String>并遇到错误:

'add(javafx.scene.control.TableColumn<capture<?>,?>)' in 'java.util.List' cannot be applied to '(javafx.scene.control.TableColumn<de.wolfsburg.stadt.beurteilungFX.controllers.tabletypes.CheckNote,java.lang.String>)'

所以这似乎没有明确的工作 由于我获得的类都实现了相同的接口,我还尝试将TableView声明为TableView<? extends MyInterface>TableView<MyInterface>,但两种情况都没有成功。
尽管他们实现了相同的接口,但是这些类的字段数和字段名都不同。

所以问题再次出现:可以有一个TableView支持同一个类和多个类的多个对象吗?

注意:我在研究过程中发现了this question,但似乎是在同一时间询问不同类的问题,而我最初要求同一类的对象,以后再更改类。

当然,替代方案是同时拥有两个TableView并隐藏其中一个。但是这会在更大的用例中消耗大量内存,其中可能需要支持10个以上的不同类。

1 个答案:

答案 0 :(得分:1)

您可以使用界面轻松完成此操作,如下所示:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TableViewViaInterface extends Application {

    private TableView<TableEntry> table ;

    @Override
    public void start(Stage primaryStage) {
        table = new TableView<>();
        BorderPane root = new BorderPane(table);
        Button showPersonTable = new Button("Person table");
        showPersonTable.setOnAction(e -> showPersonTable());
        Button showAddressTable = new Button("Address table");
        showAddressTable.setOnAction(e -> showAddressTable());

        HBox buttons = new HBox(5, showPersonTable, showAddressTable);
        buttons.setAlignment(Pos.CENTER);
        buttons.setPadding(new Insets(5));
        root.setBottom(buttons);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void showPersonTable() {
        table.getColumns().clear();
        TableColumn<TableEntry, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        table.getColumns().add(firstNameCol);

        TableColumn<TableEntry, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        table.getColumns().add(lastNameCol);

        // populate table...
    }

    private void showAddressTable() {
        table.getColumns().clear();
        TableColumn<TableEntry, String> streetCol = new TableColumn<>("Street");
        streetCol.setCellValueFactory(new PropertyValueFactory<>("street"));
        table.getColumns().add(streetCol);

        TableColumn<TableEntry, String> cityCol = new TableColumn<>("City");
        cityCol.setCellValueFactory(new PropertyValueFactory<>("city"));
        table.getColumns().add(cityCol);

        // populate table...

    }

    public interface TableEntry {}
    public class Person implements TableEntry {
        private final StringProperty firstName = new SimpleStringProperty();
        private final StringProperty lastName = new SimpleStringProperty();
        public final StringProperty firstNameProperty() {
            return this.firstName;
        }

        public final java.lang.String getFirstName() {
            return this.firstNameProperty().get();
        }

        public final void setFirstName(final java.lang.String firstName) {
            this.firstNameProperty().set(firstName);
        }

        public final StringProperty lastNameProperty() {
            return this.lastName;
        }

        public final java.lang.String getLastName() {
            return this.lastNameProperty().get();
        }

        public final void setLastName(final java.lang.String lastName) {
            this.lastNameProperty().set(lastName);
        }

    }

    public class Address implements TableEntry {
        private final StringProperty street = new SimpleStringProperty();
        private final StringProperty city = new SimpleStringProperty();
        public final StringProperty streetProperty() {
            return this.street;
        }

        public final java.lang.String getStreet() {
            return this.streetProperty().get();
        }

        public final void setStreet(final java.lang.String street) {
            this.streetProperty().set(street);
        }

        public final StringProperty cityProperty() {
            return this.city;
        }

        public final java.lang.String getCity() {
            return this.cityProperty().get();
        }

        public final void setCity(final java.lang.String city) {
            this.cityProperty().set(city);
        }



    }

    public static void main(String[] args) {
        launch(args);
    }
}

虽然我建议您在切换表时完全替换表格:

public class TableViewViaInterface extends Application {

    private BorderPane root ;
    private TableView<? extends TableEntry> table ;

    @Override
    public void start(Stage primaryStage) {
        root = new BorderPane();
        Button showPersonTable = new Button("Person table");
        showPersonTable.setOnAction(e -> showPersonTable());
        Button showAddressTable = new Button("Address table");
        showAddressTable.setOnAction(e -> showAddressTable());

        HBox buttons = new HBox(5, showPersonTable, showAddressTable);
        buttons.setAlignment(Pos.CENTER);
        buttons.setPadding(new Insets(5));
        root.setBottom(buttons);

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void showPersonTable() {

        TableView<Person> personTable = new TableView<>();
        personTable.getColumns().clear();
        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
        personTable.getColumns().add(firstNameCol);

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
        personTable.getColumns().add(lastNameCol);

        // populate table...

        this.table = personTable ;
        root.setCenter(personTable);
    }

    private void showAddressTable() {

        TableView<Address> addressTable = new TableView<>();
        addressTable.getColumns().clear();
        TableColumn<Address, String> streetCol = new TableColumn<>("Street");
        streetCol.setCellValueFactory(cellData -> cellData.getValue().streetProperty());
        addressTable.getColumns().add(streetCol);

        TableColumn<Address, String> cityCol = new TableColumn<>("City");
        cityCol.setCellValueFactory(cellData -> cellData.getValue().cityProperty());
        addressTable.getColumns().add(cityCol);

        // populate table...

        this.table = addressTable ;
        root.setCenter(addressTable);

    }

    // interface and classes as above...
}

请注意,当您切换表时,上一个表及其所有数据都有资格进行垃圾回收,因为没有对旧表的引用保留。