Chr*_*pie 4 string tableview javafx-2
我正在忙着完成一个教程并且坚持一点.
基本上在表视图中从Person类型的ObservableList中启动新场景和显示值.
person类由3个SimpleStringProperty的firsName,lastName和email组成.
但是当我运行应用程序时,它似乎在表中显示SimpleStringProperty [StringProperty [value:actualvalue]]的值,而不是SimpleStringProperty的值.如果我更改Person的类getFirstName方法以返回String firstName.get(),那么它会在表中正确显示值.
我应该使用SimpleStringProperty还是像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;
}
}
Run Code Online (Sandbox Code Playgroud)
Controller类
/*
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
和前端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>
Run Code Online (Sandbox Code Playgroud)
您需要将属性getter的名称更改为nameProperty(),其中name是您的属性.所以你会有firstNameProperty()和emailProperty().
getName() 有效,但该表不会观察属性的更改.
PropertyValueFactory的doc解释了这个主题:
在此示例中,"firstName"字符串用作Person类类型中假定的firstNameProperty()方法的引用(这是TableView项列表的类类型).此外,此方法必须返回Property实例.如果找到满足这些要求的方法,则使用此ObservableValue填充TableCell.此外,TableView将自动向返回的值添加一个观察者,以便TableView将观察到触发的任何更改,从而导致单元格立即更新.
如果不存在与此模式匹配的方法,则会尝试调用get()或is()(即上例中的getFirstName()或isFirstName()).如果存在与此模式匹配的方法,则此方法返回的值将包装在ReadOnlyObjectWrapper中并返回到TableCell.但是,在这种情况下,这意味着TableCell将无法观察ObservableValue的更改(如上面第一种方法中的情况).