JavaFx-如何在TableView中显示SimpleStringProperty值 [英] JavaFx - How to display SimpleStringProperty value in TableView

查看:101
本文介绍了JavaFx-如何在TableView中显示SimpleStringProperty值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于完成一个教程,只停留在一点上.

I am busy working through a tutorial and am stuck on one point.

基本上启动新场景并在表视图中显示Person类型的ObservableList的值.

Basically launch new scene and display values from ObservableList of type Person in a table view.

person类由3个SimpleStringProperty的firsName,lastName和email组成.

The person class consists of 3 SimpleStringProperty’s firsName, lastName and email.

但是,当我运行该应用程序时,它似乎在表中显示了SimpleStringProperty [StringProperty [value:actualvalue]]的值,而不是SimpleStringProperty的值.如果将Person的类的getFirstName方法更改为返回String firstName.get(),则它将在表中正确显示该值.

But when I run the app it seems to display the values of the SimpleStringProperty [StringProperty [value:actualvalue]] in the table and not the value of the SimpleStringProperty. If I change the Person’s class getFirstName method to return a String firstName.get() then it display the value correctly in the table.

我应该使用SimpleStringProperty还是诸如String之类的普通对象?

Am I supposed to use the SimpleStringProperty or rather plain objects like String?

人类

package co.za.chrispie.addressBook;

import javafx.beans.property.SimpleStringProperty;

public class Person {

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

public Person(final String firstName, final String lastName, final String email) {
    this.firstName = new SimpleStringProperty(firstName);
    this.lastName = new SimpleStringProperty(lastName);
    this.email = new SimpleStringProperty(email);
}

public String getFirstName() {
    return firstName.get(); //Changes this method to return a String
}

public SimpleStringProperty getLastName() {
    return lastName;
}

public SimpleStringProperty getEmail() {
    return email;
}

}

Controller类

The Controller class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.za.chrispie.addressBook;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class OracleAddressBookController implements Initializable {

@FXML
TableView<Person> table;
@FXML
TableColumn<Person, String> colFirstName;
@FXML
TableColumn<Person, String> colLastName;
@FXML
TableColumn<Person, String> colEmail;

final ObservableList<Person> data = FXCollections.observableArrayList(
    new Person("Jaco", "Pieters", "jp@me.co.za"),
    new Person("Chrispie", "Pieters", "chrispie@me.co.za")
);

@Override
public void initialize(URL url, ResourceBundle rb) {
    assert colFirstName != null : "fx:id=\"colFirstName\" was not injected: check your FXML file 'OracleAddressBook.fxml'.";
    assert colLastName != null : "fx:id=\"colLastName\" was not injected: check your FXML file 'OracleAddressBook.fxml'.";
    assert colEmail != null : "fx:id=\"colEmail\" was not injected: check your FXML file 'OracleAddressBook.fxml'.";
    configureTable();
}    

private void configureTable() {
    colFirstName.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
    colLastName.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
    colEmail.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));

    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

    table.setItems(data);
    assert table.getItems() == data;
}
}

和前端FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="500.0" prefWidth="600.0"    styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml"  fx:controller="co.za.chrispie.addressBook.OracleAddressBookController">
<children>
  <Label layoutX="14.0" layoutY="11.0" text="Address Book" />
  <TableView fx:id="table" layoutX="14.0" layoutY="35.0" prefHeight="451.0" prefWidth="572.0">
  <columns>
    <TableColumn prefWidth="75.0" text="First Name" fx:id="colFirstName" />
    <TableColumn prefWidth="75.0" text="Last Name" fx:id="colLastName" />
    <TableColumn prefWidth="75.0" text="Email" fx:id="colEmail" />
  </columns>
</TableView>
 </children>
  <stylesheets>
   <URL value="@oracleaddressbook.css" />
 </stylesheets>
</AnchorPane>

推荐答案

您需要将属性获取器的名称更改为 nameProperty(),其中 name 是您的财产.因此,您将拥有firstNameProperty()和emailProperty().

You need to change the name of your property getters to nameProperty(), where name is your property. So you would have firstNameProperty() and emailProperty().

getName() 有效,但是该表不会观察到该属性的更改.

getName() works, but the table won't observe the property's changes.

PropertyValueFactory的文档对此主题进行了说明:

The doc for PropertyValueFactory explains this subject:

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

In this example, the "firstName" string is used as a reference to an assumed firstNameProperty() method in the Person class type (which is the class type of the TableView items list). Additionally, this method must return a Property instance. If a method meeting these requirements is found, then the TableCell is populated with this ObservableValue . In addition, the TableView will automatically add an observer to the returned value, such that any changes fired will be observed by the TableView, resulting in the cell immediately updating.

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

If no method matching this pattern exists, there is fall-through support for attempting to call get() or is() (that is, getFirstName() or isFirstName() in the example above). If a method matching this pattern exists, the value returned from this method is wrapped in a ReadOnlyObjectWrapper and returned to the TableCell. However, in this situation, this means that the TableCell will not be able to observe the ObservableValue for changes (as is the case in the first approach above).

这篇关于JavaFx-如何在TableView中显示SimpleStringProperty值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆