gpt4 book ai didi

java - 为什么可以对 TableColumn 中的两种泛型类型使用相同的泛型类型?

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:27 25 4
gpt4 key购买 nike

在这个问题的答案中:Automatic row numbering in javafx table

我找到了原始问题的以下解决方案:

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

public class NumberedTableViewSample extends Application {

private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com")
);

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

@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(470);
stage.setHeight(500);

final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));

table.setEditable(true);

TableColumn numberCol = new TableColumn("#");
numberCol.setMinWidth(20);
numberCol.setCellValueFactory(new Callback<CellDataFeatures<Person, Person>, ObservableValue<Person>>() {
@Override public ObservableValue<Person> call(CellDataFeatures<Person, Person> p) {
return new ReadOnlyObjectWrapper(p.getValue());
}
});

numberCol.setCellFactory(new Callback<TableColumn<Person, Person>, TableCell<Person, Person>>() {
@Override public TableCell<Person, Person> call(TableColumn<Person, Person> param) {
return new TableCell<Person, Person>() {
@Override protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);

if (this.getTableRow() != null && item != null) {
setText(this.getTableRow().getIndex()+"");
} else {
setText("");
}
}
};
}
});
numberCol.setSortable(false);

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));

TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));

TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));

table.setItems(data);
table.getColumns().addAll(numberCol, firstNameCol, lastNameCol, emailCol);

final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);

((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}

public static class Person {

private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;

private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}

public String getFirstName() {
return firstName.get();
}

public void setFirstName(String fName) {
firstName.set(fName);
}

public String getLastName() {
return lastName.get();
}

public void setLastName(String fName) {
lastName.set(fName);
}

public String getEmail() {
return email.get();
}

public void setEmail(String fName) {
email.set(fName);
}
}

}

但是,我的问题是:为什么可以使用

TableColumn<Person, Person> and TableCell<Person, Person>

当单元格的实际类型是 TableColumn 的类型而不是 Person 时? JavaDoc 说第二个类型应该是:

S - The TableView type T - The TableColumn type

代码以某种方式工作,但是你能解释一下为什么两种类型都是 Person,尽管编号列只包含数字?这可能有助于理解代码。

据我了解,TableView 的类型应该是 Person,TableColumn 的类型应该是 String 或 int

最佳答案

第二个参数是相应列中包含的对象的类型。

正如您已经提到的,这些列通常包含从 Person 获取的单个属性,例如 StringInteger

但是,在本例中,第一列仅包含所有 Person 对象。它们不显示。您可以通过更改行来显示它们

setText(this.getTableRow().getIndex()+"");

setText(this.getTableRow().getIndex()+": "+item);

但是,您也可以使用 SomethingElse 作为第二种类型:

class SomethingElse {}

TableColumn<Person, SomethingElse> numberCol = new TableColumn<Person, SomethingElse>("#");
numberCol.setMinWidth(20);
numberCol.setCellValueFactory(new Callback<CellDataFeatures<Person, SomethingElse>, ObservableValue<SomethingElse>>() {
@Override public ObservableValue<SomethingElse> call(CellDataFeatures<Person, SomethingElse> p) {
return new ReadOnlyObjectWrapper<SomethingElse>(new SomethingElse());
}
});

numberCol.setCellFactory(new Callback<TableColumn<Person, SomethingElse>, TableCell<Person, SomethingElse>>() {
@Override public TableCell<Person, SomethingElse> call(TableColumn<Person, SomethingElse> param) {
return new TableCell<Person, SomethingElse>() {
@Override protected void updateItem(SomethingElse item, boolean empty) {
super.updateItem(item, empty);

if (this.getTableRow() != null && item != null) {
setText(this.getTableRow().getIndex()+"");
} else {
setText("");
}
}
};
}
});

它仅用于枚举行(顺便说一下,这是一种令人困惑的复杂方法)

关于java - 为什么可以对 TableColumn<T, S> 中的两种泛型类型使用相同的泛型类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24741609/

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