SimpleStringProperty和SimpleIntegerProperty TableView JavaFX

SimpleStringProperty和SimpleIntegerProperty TableView JavaFX,第1张

SimpleStringProperty和SimpleIntegerProperty TableView JavaFX

您不需要在表数据对象中使用属性来显示它们,尽管在某些情况下最好使用属性。

以下代码将显示一个基于Person类的人员表,该类仅具有String字段。

import javafx.application.Application;import javafx.collections.*;import javafx.geometry.Insets;import javafx.scene.*;import javafx.scene.control.*;import javafx.scene.control.cell.PropertyValueFactory;import javafx.scene.layout.VBox;import javafx.scene.text.Font;import javafx.stage.Stage;public class ReadonlyTableView 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) {    stage.setTitle("Table View Sample");    stage.setWidth(450);    stage.setHeight(500);    final Label label = new Label("Address Book");    label.setFont(new Font("Arial", 20));    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(firstNameCol, lastNameCol, emailCol);    final VBox vbox = new VBox();    vbox.setSpacing(5);    vbox.setPadding(new Insets(10, 0, 0, 10));    vbox.getChildren().addAll(label, table);    stage.setScene(new Scene(new Group(vbox)));    stage.show();  }  public static class Person {    private String firstName;    private String lastName;    private String email;    private Person(String fName, String lName, String email) {      this.firstName = fName;      this.lastName = lName;      this.email = email;    }    public String getFirstName() { return firstName; }    public void setFirstName(String fName) { firstName = fName; }    public String getLastName() { return lastName; }    public void setLastName(String lName) { lastName = lName; }    public String getEmail() { return email; }    public void setEmail(String inMail) { email = inMail; }  }}

说明

使用Properties和ObservableLists的目的是使它们成为可听元素。使用属性时,如果数据模型中的属性属性值更改,则TableView中的项目视图将自动更新以匹配更新的数据模型值。例如,如果某人的电子邮件属性的值设置为新值,则该更新将反映在TableView中,因为它侦听属性更改。如果改为使用纯字符串表示电子邮件,则TableView将不会刷新,因为它不会意识到电子邮件值的更改。

该PropertyValueFactory文档详细描述了这一过程:

如何使用此类的一个示例是:

TableColumn<Person,String> firstNameCol = new

TableColumn(“First Name”);
firstNameCol.setCellValueFactory(new
PropertyValueFactory(“firstName”));

在此示例中,“
firstName”字符串用作对Person类类型(TableView项目列表的类类型)中假定的firstNameProperty()方法的引用。此外,此方法必须返回一个Property实例。如果找到满足这些要求的方法,则用此Observablevalue填充TableCell。此外,TableView会自动将观察者添加到返回的值中,这样,触发的任何更改都将被TableView观察到,从而导致单元立即更新。

如果不存在与该模式匹配的方法,则尝试调用get()或is()(即上例中的getFirstName()或isFirstName())具有透支支持。如果存在与该模式匹配的方法,则从该方法返回的值将包装在ReadOnlyObjectWrapper中,并返回到TableCell。但是,在这种情况下,这意味着TableCell将无法观察Observablevalue的更改(与上述第一种方法一样)。

更新资料

这是与第一个示例的对比示例,该示例演示TableView如何观察并基于对项目的ObservableList的更改以及对基于属性的item属性值的更改自动刷新。

import javafx.application.Application;import javafx.beans.property.*;import javafx.collections.*;import javafx.event.*;import javafx.geometry.Insets;import javafx.scene.*;import javafx.scene.control.*;import javafx.scene.control.cell.PropertyValueFactory;import javafx.scene.layout.VBox;import javafx.scene.text.Font;import javafx.stage.Stage;public class PropertybasedTableView extends Application {  private TableView<Person> table = new TableView<Person>();  private final ObservableList<Person> data = FXCollections.observableArrayList();  private void initData() {    data.setAll(      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) {    initData();    stage.setTitle("Table View Sample");    stage.setWidth(450);    stage.setHeight(500);    final Label label = new Label("Address Book");    label.setFont(new Font("Arial", 20));    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(firstNameCol, lastNameCol, emailCol);    table.setPrefHeight(300);    final Button setEmailButton = new Button("Set first email in table to wizard@frobozz.com");    setEmailButton.setonAction(new EventHandler<ActionEvent>() {      @Override public void handle(ActionEvent event) {        if (data.size() > 0) {          data.get(0).setEmail("wizard@frobozz.com");        }        }    });    final Button removeRowButton = new Button("Remove first row from the table");    removeRowButton.setonAction(new EventHandler<ActionEvent>() {      @Override public void handle(ActionEvent event) {        if (data.size() > 0) {          data.remove(0);        }        }    });    final Button resetButton = new Button("Reset table data");    resetButton.setonAction(new EventHandler<ActionEvent>() {      @Override public void handle(ActionEvent event) {        initData();      }    });    final VBox vbox = new VBox(10);    vbox.setPadding(new Insets(10, 0, 0, 10));    vbox.getChildren().addAll(label, table, setEmailButton, removeRowButton, resetButton);    stage.setScene(new Scene(new Group(vbox)));    stage.show();  }  public static class Person {    private final StringProperty firstName;    private final StringProperty lastName;    private final StringProperty 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 StringProperty firstNameProperty() { return firstName; }    public String getLastName() { return lastName.get(); }    public void setLastName(String lName) { lastName.set(lName); }    public StringProperty lastNameProperty() { return lastName; }    public String getEmail() { return email.get(); }    public void setEmail(String inMail) { email.set(inMail); }    public StringProperty emailProperty() { return email; }  // if this method is commented out then the tableview will not refresh when the email is set.  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5461952.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存