JavaFx - How to display SimpleStringProperty value in TableView

I am busy working on a textbook and stuck at one point.

Basically start new scenes and display values ​​from an ObservableList of type Person in a table view.

The person class consists of 3 SimpleStringPropertys firsName, lastName and email.

But when I run the application, it seems to display the SimpleStringProperty [StringProperty [value: actual value]] values ​​in the table, not the SimpleStringProperty value. If I changed the getFirstName method of the Person class to return String firstName.get (), then it will correctly display the value in the table.

Should I use SimpleStringProperty or fairly simple objects like String? enter image description here

Class man

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

and front end 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> 
+4
source share
1 answer

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

getName () works , but the table does not see property changes.

The document for PropertyValueFactory explains this question:

In this example, the firstName string is used as a reference to the alleged firstNameProperty () method in the class of the Person class (which is the type of the TableView item list class). In addition, this method should return an instance of Property. If a method satisfying these requirements is found, then the TableCell is populated with this ObservableValue. In addition, TableView will automatically add an observer to the return value, so that any changed changes will be observed using the TableView, which will lead to an immediate cell update.

If no method matching this pattern exists, there is an error trying to call get () or is () (i.e. getFirstName () or isFirstName () in the example above). If there is a method matching this pattern, the value returned by this method is verified with ReadOnlyObjectWrapper and returned to the TableCell. However, in this situation, this means that TableCell will not be able to observe the ObservableValue for changes (as is the case in the first approach above).

+8
source

Source: https://habr.com/ru/post/1447802/


All Articles