gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:39:05 24 4
gpt4 key购买 nike

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

我面临的具体情况是一个 TableView,我想用它来显示搜索结果。这些搜索可能会产生不同的“对象”作为结果,比如说强制性的 PersonAddress 。当用户搜索其中任何一个时,结果将是一组 PersonAddress .
搜索后,表格将相应更改( getItems().clear();getColumns().clear(); ),但是我已经陷入了执行填充的困境。
我将 TableView 声明为 TableView<?> searchResults这是用 FXML 初始化的。现在我尝试添加 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>)'

所以这似乎并没有明显发挥作用。
由于我得到的类都实现相同的接口(interface),我也尝试将 TableView 声明为 TableView<? extends MyInterface>TableView<MyInterface>在这两种情况下都没有成功。
尽管它们实现相同的接口(interface),但类在字段数量和字段名称方面有所不同。

那么问题又来了:是否可以拥有一个支持同一类和多个类的多个对象的TableView?

注意:我发现this question在研究过程中,但似乎是一个同时询问不同类别的问题,而我的问题首先要求同一类别的对象,然后再更改类别。

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

最佳答案

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

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...
}

请注意,当您切换表时,前一个表及其所有数据都将适合垃圾回收,因为没有保留对旧表的引用。

关于java - 整个应用程序中不同对象的 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45011460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com