JavaFX:属性Property简介
属性介绍
在JavaFX中,属性(Properties)是JavaFX Beans规范的一部分,它们是对Java标准Bean属性的增强,支持绑定、监听和无效化通知等特性。JavaFX属性使得在UI控件和数据模型之间建立双向绑定变得非常方便。
属性核心概念
属性(Property)
一个属性是一个可观察的对象,它包装了一个值,并且可以监听该值的变化。属性通常以“Property”结尾,例如`StringProperty`、`IntegerProperty`等。
绑定(Binding)
绑定允许一个属性的值自动根据另一个或多个属性的值进行计算和更新。绑定可以是单向的(unidirectional)或双向的(bidirectional)。
监听器(Listener)
当属性值发生变化时,可以触发监听器(ChangeListener或InvalidationListener)。
可观察对象(Observable)
属性实现了“Observable”接口,可以添加监听器来监听变化。
属性类型
JavaFX提供了多种属性类型,用于包装基本类型和对象:
BooleanProperty
IntegerProperty
DoubleProperty
StringProperty
ObjectProperty<T>(用于任意对象)
以及对应的只读版本,如“ReadOnlyIntegerProperty”等。
创建属性
通常,属性不会直接创建,而是作为某个类的成员。
Book类
package learnjavafx8.ch02.book;import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;/*** @copyright 2003-2024* @author qiao wei* @date 2024-08-11* @version 1.0* @brief Bean测试。类Book所以字段均使用属性字段,确保可及时更新。* @history name* date* brief*/
public class Book {public Book() {this("Unknown", 0, "Unknown");}public Book(String title,double price,String isbn) {this.titleProperty = new SimpleStringProperty(this, "titleProperty", title);this.priceProperty = new SimpleDoubleProperty(this, "priceProperty", price);this.isbnWrapper = new ReadOnlyStringWrapper(this, "isbnWrapper", isbn);}public StringProperty titleProperty() {return titleProperty;}public void setTitleProperty(StringProperty title) {this.titleProperty = title;}public String title() {return titleProperty.get();}public void setTitle(String title) {titleProperty.set(title);}public DoubleProperty priceProperty() {return priceProperty;}public void setPriceProperty(DoubleProperty property) {this.priceProperty = property;}public double price() {return priceProperty.get();}public void setPrice(double price) {priceProperty.set(price);}private StringProperty titleProperty;private DoubleProperty priceProperty;private ReadOnlyStringWrapper isbnWrapper;
}
测试类
package learnjavafx8.ch02.book;import org.junit.jupiter.api.Test;import javafx.beans.property.ReadOnlyProperty;/* ** @copyright 2003-2025* @author qiao wei* @date 2025-06-28* @version 1.0* @brief * @history name* date* brief*/
class BookTest {@Testpublic void test01() {Book book = new Book("Harnessing JavaFX", 9.99, "0123456789");System.out.println("After creating the Book010 object...");// Print Property detailsprintDetails(book.titleProperty());printDetails(book.priceProperty());// Change the book's propertiesbook.setTitle("Harnessing JavaFX 8.0");book.setPrice(9.49);// Print Property detailsprintDetails(book.titleProperty());printDetails(book.priceProperty());}private static void printDetails(ReadOnlyProperty<?> property) {String name = property.getName();Object value = property.getValue();Object bean = property.getBean();String beanClassName = (null == bean) ? "null" : bean.getClass().getSimpleName();String propertyClassName = property.getClass().getSimpleName();System.out.print(propertyClassName);System.out.print("[Name:" + name);System.out.print(", Bean Class:" + beanClassName);System.out.println(", Value:" + value + "]");}
}
测试结果
After creating the Book010 object...
SimpleStringProperty[Name:titleProperty, Bean Class:Book, Value:Harnessing JavaFX]
SimpleDoubleProperty[Name:priceProperty, Bean Class:Book, Value:9.99]
SimpleStringProperty[Name:titleProperty, Bean Class:Book, Value:Harnessing JavaFX 8.0]
SimpleDoubleProperty[Name:priceProperty, Bean Class:Book, Value:9.49]