JavaFX提供了Observable接口和ObservableValue接口, 处理invalidated(数据失效)和changed(数据更新)事件。
示例一:
自定义类继承InvalidationListener接口,当数据失效时,触发该接口的invalidated事件。
Book数据类,其中的变量分别是StringProperty类型和DoubleProperty类型。分别注册InvalidationListener接口的实现ObservableEvent类。
package javafx8.ch03.observablelist;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* @copyright 2003-2023
* @package javafx8.ch03.observablelist
* @file Book.java
* @date 2023-12-22 16:13
* @author qiao wei
* @version 1.0
* @brief
* @history
*/
public class Book {
public Book(String isbn, String bookTitle, double price) {
isbnProperty = new SimpleStringProperty(this, "ISBN", isbn);
bookTitleProperty = new SimpleStringProperty(this, "Book", bookTitle);
priceProperty = new SimpleDoubleProperty(this, "Price", price);
observableEvent = new ObservableEvent();
isbnProperty.addListener(observableEvent::invalidated);
bookTitleProperty.addListener(observableEvent::invalidated);
priceProperty.addListener(observableEvent::invalidated);
}
public String getIsbn() {
return isbnProperty.get();
}
public void setIsbn(String isbn) {
isbnProperty.set(isbn);
}
public String getBookTitle() {
return bookTitleProperty.get();
}
public void setBookTitle(String bookTitle) {
bookTitleProperty.set(bookTitle);
}
public double getPrice() {
return priceProperty.get();
}
public void setPrice(double price) {
priceProperty.set(price);
}
public static final String ISBN = "ISBN";
public static final String BOOK = "Book";
public static final String PRICE = "Price";
private StringProperty isbnProperty;
private StringProperty bookTitleProperty;
private DoubleProperty priceProperty;
private ObservableEvent observableEvent;
}
ObservableEvent类,继承InvalidationListener接口:
package javafx8.ch03.observablelist;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.StringProperty;
/**
* @copyright 2003-2023
* @package javafx8.ch03.observablelist
* @file ObservableEvent.java
* @date 2023-12-22 16:22
* @author qiao wei
* @version 1.0
* @brief
* @history
*/
public class ObservableEvent implements InvalidationListener {
@Override
public void invalidated(Observable observable) {
String name = null;
if (observable instanceof StringProperty) {
name = ((StringProperty) observable).getName();
} else if (observable instanceof DoubleProperty) {
name = ((DoubleProperty) observable).getName();
}
switch (name) {
case Book.ISBN:
System.out.println("ISBN------------: " + ((StringProperty) observable).getName());
break;
case Book.BOOK:
System.out.println("OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
break;
case Book.PRICE:
System.out.println(((DoubleProperty) observable).get());
break;
default:
break;
}
}
}
测试代码:
package javafx8.ch03.observablelist;
/**
* @copyright 2003-2023
* @package javafx8.ch03.observablelist
* @file BookTest.java
* @date 2023-12-22 16:35
* @author qiao wei
* @version 1.0
* @brief
* @history
*/
public class BookTest {
public static void main(String[] args) {
test01();
}
private static void test01() {
Book book01 = new Book("123", "Test01", 22.55D);
book01.setIsbn("222---");
book01.setPrice(22.3d);
}
}
运行结果:
ISBN------------: ISBN
22.3
Process finished with exit code 0
程序更新String类型、double类型数据,触发invalidated事件。