javafx - FX assessing if ListView is empty using different change listeners -
i want assess whether listview empty disable or enable submit button. in essence, when listview has no content button disabled , when has 1 or more elements enabled.
i've managed achieve using listchangelistener, using code such as:
vp.addlistchangelistener(change -> { change.next(); if (change.getlist().size() == 0 && change.wasremoved() || change.getlist().size() == 1 && change.wasadded()) { bp.togglesubmitavailability("submit"); } });
where method addlistchangelistener follows:
public void addlistchangelistener(listchangelistener<name> listener) { names.addlistener(listener); }
what ideally wanted use traditional changelistener , listen 1 of properties associated listview. example, thought may work:
vp.additemslistener((observable, oldvalue, newvalue) -> { if (newvalue.size() == 0 && oldvalue.size() > 0 || newvalue.size() == 1 && oldvalue.size() == 0) { bp.togglesubmitavailability("submit"); } });
where method additemslistener follows (and attaches listener itemsproperty):
public void additemslistener(changelistener<observablelist<name>> listener) { listview.itemsproperty().addlistener(listener); }
however, reason, when add items listview, or add items observablelist attached it, property not being changed, nothing happening - triggers itemsproperty of listview change, , if approach not work, there way did listchangelistener utilising property associated listview (in reasonably elegant way of course)?
the itemsproperty()
property holds list itself; listeners registered notified if call listview.setitems(...)
, (i.e. if list reference changed) not notified if modify content of list contains (i.e. if listview.getitems().add(...)
).
the bindings
class provides isempty(observablelist)
method can use here:
button.disableproperty().bind(bindings.isempty(listview.getitems()));
or equivalently, listener:
bindings.isempty(listview.getitems()).addlistener( (obs, wasempty, isnowempty) -> button.setdisable(isnowempty));
so implement method this:
public void addemptylistlistener(changelistener<boolean> listener) { bindings.isempty(listview.getitems()).addlistener(listener); }
and do
vp.addemptylistlistener((obs, wasempty, isnowempty) -> { // ... });
Comments
Post a Comment