Listview JavaFX-组合框中的嵌套列表

Listview JavaFX-组合框中的嵌套列表,listview,binding,combobox,javafx,nested-lists,Listview,Binding,Combobox,Javafx,Nested Lists,在JavaFX8中,如何将具有嵌套员工的主管列表绑定到单个组合框 我与员工和主管有以下简单的班级结构: public class Employee { private final StringProperty nameProperty = new SimpleStringProperty(); public Employee(String name) { this.nameProperty.set(name); } public St

在JavaFX8中,如何将具有嵌套员工的主管列表绑定到单个组合框

我与员工和主管有以下简单的班级结构:

public class Employee 
{
    private final StringProperty nameProperty = new SimpleStringProperty();

    public Employee(String name)
    {
        this.nameProperty.set(name);
    }

    public StringProperty nameProperty()
    {
        return 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()
    {
        return this.nameProperty;
    }

    public ListProperty<Employee> employeesProperty()
    {
        return this.employeesProperty;
    }
}
如果我只是将combobox.itemsProperty绑定到Supervisors.Supervisors属性,那么我只会得到主管的列表,而不是员工的列表

注意:我使用的是绑定,因此添加或删除主管或员工将直接反映在组合框中

我是使用特殊的绑定还是使用某种适配器类将嵌套列表“展开”为主管和员工的单个列表?如果是后者,那么我如何跟踪对主管列表(和嵌套员工)的更改,以及如何防止它变成注册和取消注册的ChangeListener的复杂混乱


PS:如果答案中包含一种始终在组合框中显示主管/员工正确姓名的方法,则会有一个神奇的独角兽奖励积分-因此,如果姓名发生更改,则组合框中的姓名也会发生更改。

我最终自己制定了一个糟糕而复杂的解决方案

我为主管和员工添加了一个基类:

public abstract class NamedInstance {
    private final StringProperty nameProperty = new SimpleStringProperty();

    public NamedInstance(String name)
    {
        this.nameProperty.set(name);
    }

    public StringProperty nameProperty()
    {
        return nameProperty;
    }

    public String getName()
    {
        return nameProperty.get();
    }

    @Override
    public String toString() {
        return getName();
    }
}
toString()将自动用于组合框列表中

然后,我添加了这个糟糕的适配器类,用于制作嵌套的“带员工的主管”结构的平面列表版本。在嵌套列表中添加或删除主管或员工时,平面列表将自动更改:

public class SupervisorsAndEmployeesList
{
    // the flat list of supervisors and employees
    private final ListProperty<NamedInstance> supervisorsAndEmployees = new SimpleListProperty<NamedInstance>(FXCollections.observableArrayList());
    // a ChangeListener for changes in a 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 a ChangeListener for changes in the 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)
            {
                // make sure to have listeners on each employee, and to remove the listeners 
                // again when supervisors are removed
                for (Supervisor supervisor : oldValue)
                {
                    supervisor.employeesProperty().removeListener(employeeChangeListener);
                }
                for (Supervisor supervisor : newValue)
                {
                    supervisor.employeesProperty().addListener(employeeChangeListener);
                }
                refreshList();
            }
        });
    }

    private void refreshList() 
    {
        // clear the list and re-add all Supervisors and Employees. 
        // this is not effective, but more readable than keeping track 
        // a each change in the list
        supervisorsAndEmployees.clear();
        for (Supervisor supervisor : Supervisors.supervisorsProperty().get())
        {
            supervisorsAndEmployees.add(supervisor);
            for (Employee employee : supervisor.employeesProperty().get())
            {
                supervisorsAndEmployees.add(employee);
            }
        }
    }

    public ListProperty<NamedInstance> supervisorsAndEmployeesProperty()
    {
        return supervisorsAndEmployees;
    }
}
处理这样的任务需要大量的代码


我还修复了问题,在组合框中的名称在何时更改主管或员工名称时都会发生变化,但我没有在上面包含该代码,因为这会使代码更加复杂和不可读。不过,一般来说,您“只是”在每个主管和员工的nameProperty中添加一个“nameChangeListener”(记住也要像我处理employeeChangeListener一样删除侦听器)。每次调用nameChangeListener时,您都会调用“refreshList()”

是否在同一个组合框中查找主管和员工?是。我可能需要一个包含name属性的主管和员工共享的基本Person类,所以它应该是ComboBox——但我真正头疼的是,如何在ComboBox和主管和嵌套员工列表之间进行绑定(如果可能的话,以一种简洁的方式)?有没有一种简单的方法可以做到这一点,或者我是否需要将该列表“展开”为一个扁平的人员列表,然后绑定到该列表?(这似乎比它需要的更复杂,我必须考虑让这个简单的列表保持最新)
public abstract class NamedInstance {
    private final StringProperty nameProperty = new SimpleStringProperty();

    public NamedInstance(String name)
    {
        this.nameProperty.set(name);
    }

    public StringProperty nameProperty()
    {
        return nameProperty;
    }

    public String getName()
    {
        return nameProperty.get();
    }

    @Override
    public String toString() {
        return getName();
    }
}
public class SupervisorsAndEmployeesList
{
    // the flat list of supervisors and employees
    private final ListProperty<NamedInstance> supervisorsAndEmployees = new SimpleListProperty<NamedInstance>(FXCollections.observableArrayList());
    // a ChangeListener for changes in a 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 a ChangeListener for changes in the 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)
            {
                // make sure to have listeners on each employee, and to remove the listeners 
                // again when supervisors are removed
                for (Supervisor supervisor : oldValue)
                {
                    supervisor.employeesProperty().removeListener(employeeChangeListener);
                }
                for (Supervisor supervisor : newValue)
                {
                    supervisor.employeesProperty().addListener(employeeChangeListener);
                }
                refreshList();
            }
        });
    }

    private void refreshList() 
    {
        // clear the list and re-add all Supervisors and Employees. 
        // this is not effective, but more readable than keeping track 
        // a each change in the list
        supervisorsAndEmployees.clear();
        for (Supervisor supervisor : Supervisors.supervisorsProperty().get())
        {
            supervisorsAndEmployees.add(supervisor);
            for (Employee employee : supervisor.employeesProperty().get())
            {
                supervisorsAndEmployees.add(employee);
            }
        }
    }

    public ListProperty<NamedInstance> supervisorsAndEmployeesProperty()
    {
        return supervisorsAndEmployees;
    }
}
supervisorsAndEmployeesComboBox.setItems(new SupervisorsAndEmployeesList().supervisorsAndEmployeesProperty().get());