【问题标题】:JavaFX - nested lists in comboboxJavaFX - 组合框中的嵌套列表
【发布时间】:2014-08-13 02:03:15
【问题描述】:

如何将具有嵌套员工的主管列表绑定到 JavaFX 8 中的单个组合框。

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

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;
    }
}

最后是一个简单的静态主管类:

public static class Supervisors
{
    private static final ListProperty<Supervisor> supervisorsProperty = new SimpleListProperty<Supervisor>();

    public static ListProperty<Supervisor> supervisorsProperty()
    {
        return supervisorsProperty;
    }
}

(本例代码已简化)

现在,我的问题是:

如何将主管列表绑定到 JavaFX 组合框(或列表视图,区别不重要)?我想“展开”员工列表,以便组合框项目如下所示:

Supervisor A
Employee A.1
Employee A.2
Supervisor B
Employee B.1

如果我只是将 combobox.itemsProperty 绑定到 Supervisors.supervisorsProperty,那么我只会得到主管列表,而不是员工。

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

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

PS:如果答案包括始终在组合框中显示主管/员工的正确姓名的方式,则有一个神奇的独角兽奖励积分 - 这样,如果更改名称,则组合框中的名称也会更改.

【问题讨论】:

  • 您是否在同一个 ComboBox 中寻找主管和员工?
  • 是的。我可能需要一个共享的基础 Person 类,用于其中包含 name 属性的 Supervisor 和 Employee,所以它是 ComboBox - 但我真的很头疼,我该如何进行绑定(如果可能的话,以一种简洁明了的方式)在组合框与主管和嵌套员工列表之间?有没有一种简单的方法可以做到这一点,还是我需要将该列表“展开”成一个平面 Persons 列表然后绑定到该列表? (这似乎比它需要的更复杂,我必须考虑让这个平面列表保持最新)

标签: listview binding combobox javafx nested-lists


【解决方案1】:

我最终自己做出了糟糕而复杂的解决方案。

我为 Supervisor 和 Employee 添加了一个基类:

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() 将自动用于 ComboBox 列表中。

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

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;
    }
}

ComboBox 项设置如下:

supervisorsAndEmployeesComboBox.setItems(new SupervisorsAndEmployeesList().supervisorsAndEmployeesProperty().get()); 

处理这样的任务需要大量的代码......

我还解决了每当主管或员工名称更改时组合框中的名称更改的问题,但我没有在上面包含该代码,因为它会使代码更加复杂和不可读。不过,一般来说,您“只是”向每个主管和员工的 nameProperty 添加一个“nameChangeListener”(请记住,也像我对employeeChangeListener 所做的那样删除监听器)。每次调用 nameChangeListener 时,都会调用“refreshList()”。

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2015-11-14
    相关资源
    最近更新 更多