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

Ber*_*ker 2 java javafx javafx-8

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

因此,当我尝试设置日历字段时Calendar.YEAR,有一个更好的解决方案

Calendar c = duedateCalendar.get();
c.set(Calendar.YEAR,2017);
duedateCalendar.set(c);
Run Code Online (Sandbox Code Playgroud)

Jam*_*s_D 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)
Run Code Online (Sandbox Code Playgroud)

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

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

Calendar c = duedateCalendar.get();
Calendar newDueDate = Calendar.getInstance();
newDueDate.setTime(c.getTime());
newDueDate.set(Calendar.YEAR, 2017);
duedateCalendar.set(newDueDate);
Run Code Online (Sandbox Code Playgroud)

另外,我强烈建议使用java.timeAPI(例如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));
Run Code Online (Sandbox Code Playgroud)