如何修改调用更改侦听器的javafx对象属性的对象?

时间:2016-06-22 18:57:05

标签: java javafx javafx-8

我有javafx.beans.property.ObjectProperty<Calendar>。如何修改日历,以便javafx注册它并调用更改侦听器?

因此,当我尝试设置日历字段Calendar.YEAR时,有一个比

更好的解决方案
Calendar c = duedateCalendar.get();
c.set(Calendar.YEAR,2017);
duedateCalendar.set(c);

1 个答案:

答案 0 :(得分:9)

如果您传递的值等于当前值,则设置属性值将是无操作,在这种情况下,它显然是:

Calendar c = duedateCalendar.get();
// c is now a copy of the reference held internally by duedateCalendar

c.set(Calendar.YEAR,2017);
// this updates the Calendar object referenced both by c and internally
// by duedateCalendar. Since the YEAR field in Calendar is just a primitive,
// and is not an observable value in the JavaFX sense, nothing is notified
// of the change

duedateCalendar.set(c);
// the value passed to set here is equal (indeed identical) to the value
// held internally by duedateCalendar (since it's an actual copy of the
// same reference). Internally, duedateCalendar will compare the parameter
// to the value it already holds, will see that they are the same, and
// so will take no action (including not notifying listeners)

因此,不会触发任何更改事件(实际上没有任何可观察的更改)。

您可以创建一个代表新日期的新Calendar并将其设置为:

Calendar c = duedateCalendar.get();
Calendar newDueDate = Calendar.getInstance();
newDueDate.setTime(c.getTime());
newDueDate.set(Calendar.YEAR, 2017);
duedateCalendar.set(newDueDate);

另外,我强烈建议您使用java.time API(例如LocalDateLocalDateTime)而不是遗留Calendar类。此API中的类很大程度上是不可变的(因此它们可以更好地与JavaFX属性类一起工作),并且具有返回相应类的新实例的函数方法。例如,LocalDate是不可变的,并且具有诸如LocalDate.plusYears(...)之类的方法,这些方法返回对新LocalDate对象的引用。所以你可以这样做:

// just create a random date sometime in 2016:
Random rng = new Random();
LocalDate random2016Date = LocalDate.of(2016, 1, 1).plusDays(rng.nextInt(365));

// general listener for logging:
ChangeListener<Object> listener = (obs, oldVal, newVal) -> System.out.println(oldVal + " -> " + newVal);


ObjectProperty<LocalDate> date = new SimpleObjectProperty<>(random2016Date);
date.addListener(listener);

// add one year to the current date:
date.set(date.get().plusYears(1));