Tuesday, 15 July 2014

listview - JavaFX - nested lists in combobox -



listview - JavaFX - nested lists in combobox -

how bind list of supervisors nested employees single combobox in javafx 8.

i have next simple class construction employees , supervisors:

public class employee { private final stringproperty nameproperty = new simplestringproperty(); public employee(string name) { this.nameproperty.set(name); } public stringproperty nameproperty() { homecoming this.nameproperty; } } public class supervisor { private final stringproperty nameproperty = new simplestringproperty(); private final listproperty<employee> employeesproperty = new simplelistproperty<employee>(); public supervisor(string name, list<employee> employees) { this.nameproperty.set(name); this.employeesproperty.addall(employees); } public stringproperty nameproperty() { homecoming this.nameproperty; } public listproperty<employee> employeesproperty() { homecoming this.employeesproperty; } }

and simple static supervisors class:

public static class supervisors { private static final listproperty<supervisor> supervisorsproperty = new simplelistproperty<supervisor>(); public static listproperty<supervisor> supervisorsproperty() { homecoming supervisorsproperty; } }

(the code has been simplified example)

now, question is:

how bind list of supervisors javafx combobox (or listview, difference shouldn't matter)? "unfold" list of employees, combobox items this:

supervisor employee a.1 employee a.2 supervisor b employee b.1

if bind combobox.itemsproperty supervisors.supervisorsproperty, list of supervisors, not employees.

note: using binding, adding or removing supervisors or employees reflected straight in combobox.

do utilize special binding or utilize kind adapter class "unfold" nested lists single list of supervisors , employees? if latter, how maintain track of changes supervisors list (and nested employees), , how maintain becoming complex mess of changelisteners beingness registered , unregistered?

ps: there magic unicorn bonus points if reply includes way of having right name of supervisor/employee displayed in combobox - if name changed, name changed in combobox.

i ended making awful , complex solution myself.

i added base of operations class both supervisor , employee:

public abstract class namedinstance { private final stringproperty nameproperty = new simplestringproperty(); public namedinstance(string name) { this.nameproperty.set(name); } public stringproperty nameproperty() { homecoming nameproperty; } public string getname() { homecoming nameproperty.get(); } @override public string tostring() { homecoming getname(); } }

the tostring() automatically used in combobox list.

i added awful adapter class making flat list version of nested "supervisor employees" structure. flat list automatically alter when supervisors or employees added or removed nested list:

public class supervisorsandemployeeslist { // flat list of supervisors , employees private final listproperty<namedinstance> supervisorsandemployees = new simplelistproperty<namedinstance>(fxcollections.observablearraylist()); // changelistener changes in list of employees private final changelistener<observablelist<employee>> employeechangelistener = new changelistener<observablelist<employee>>() { @override public void changed(observablevalue<? extends observablelist<employee>> observable, observablelist<employee> oldvalue, observablelist<employee> newvalue) { refreshlist(); } }; public supervisorsandemployeeslist() { // add together changelistener changes in list of supervisors supervisors.supervisorsproperty().addlistener(new changelistener<observablelist<supervisor>>() { @override public void changed( observablevalue<? extends observablelist<supervisor>> observable, observablelist<supervisor> oldvalue, observablelist<supervisor> newvalue) { // create sure have listeners on each employee, , remove listeners // 1 time again when supervisors removed (supervisor supervisor : oldvalue) { supervisor.employeesproperty().removelistener(employeechangelistener); } (supervisor supervisor : newvalue) { supervisor.employeesproperty().addlistener(employeechangelistener); } refreshlist(); } }); } private void refreshlist() { // clear list , re-add supervisors , employees. // not effective, more readable keeping track // each alter in list supervisorsandemployees.clear(); (supervisor supervisor : supervisors.supervisorsproperty().get()) { supervisorsandemployees.add(supervisor); (employee employee : supervisor.employeesproperty().get()) { supervisorsandemployees.add(employee); } } } public listproperty<namedinstance> supervisorsandemployeesproperty() { homecoming supervisorsandemployees; } }

the combobox items set this:

supervisorsandemployeescombobox.setitems(new supervisorsandemployeeslist().supervisorsandemployeesproperty().get());

an awful lot of code handle such task...

i fixed issue having name in combobox alter whenever supervisor or employee name change, did not include code in above, because create code more complex , unreadable. though, "just" add together "namechangelistener" nameproperty of each supervisor , employee (remember remove listener employeechangelistener). each time namechangelistener invoked, phone call "refreshlist()".

listview binding combobox javafx nested-lists

No comments:

Post a Comment