Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于JavaFXObservableList的元素行为_Java_List_Javafx_Observablelist - Fatal编程技术网

基于JavaFXObservableList的元素行为

基于JavaFXObservableList的元素行为,java,list,javafx,observablelist,Java,List,Javafx,Observablelist,我有一个JavaFX应用程序,它有几个基于相同泛型类型的元素(一个ListView,两个ChoiceBox) @FXML private ListView<Department> departmentList; @FXML private ChoiceBox<Department> employeeAddDepartment; @FXML private ChoiceBox<Department> employeeEditDepartment; ,它也会自动

我有一个JavaFX应用程序,它有几个基于相同泛型类型的元素(一个ListView,两个ChoiceBox)

@FXML private ListView<Department> departmentList;
@FXML private ChoiceBox<Department> employeeAddDepartment;
@FXML private ChoiceBox<Department> employeeEditDepartment;
,它也会自动添加到ChoiceBox中,并且在listview和两个ChoiceBox中会得到3个重复项。 为什么会这样?我试着装出一副呆头呆脑的样子。列出文档,但没有找到任何相关内容,也搜索了stackoverflow。我试图将equals/hashCode方法添加到Employee类中,但似乎没有影响它

更奇怪的是,有时同步停止工作。就像上次启动它时一样,它工作正常,但今天您根本看不到添加到ChoiceBox的项目(如果您仅向其中一个元素添加元素以防止重复)。 因此,如果由于某种原因,选择框今天没有同步,您可以尝试修改代码并尝试将元素添加到选择框中。 接下来,启动应用程序并再次看到3个重复项。您恢复了代码,现在您的应用程序就像昨天一样工作。 这听起来很奇怪,但实际上是这样的


所以,问题是为什么会发生/不会随机发生,以及如何禁用/启用它以确保它能按预期在下一次启动时工作?

发生这种情况是因为这三个组件共享相同的列表。这样创建它:

List<Department> ds = db.getDepartmentList();
departmentList.getItems().addAll(ds);
employeeAddDepartment.getItems().addAll(ds);
employeeEditDepartment.getItems().addAll(ds);
List ds=db.getDepartmentList();
departmentList.getItems().addAll(ds);
employeeAddDepartment.getItems().addAll(ds);
employeeEditDepartment.getItems().addAll(ds);

您只有一个列表。你添加的任何东西都会影响这三个变量,因为它们都引用同一个对象。是的,但看最后一段,它停止了工作。为什么?除非您提供足够的代码来重现该部分(即a),否则没有人可能回答该部分(零星部分)。这种行为不是由你发布的代码引起的。是的,看起来像是一个非常个别的案例,因为它起作用了,现在没有任何代码更改就不会发生。。。
departmentList.getItems().add(dep);
List<Department> ds = db.getDepartmentList();
departmentList.getItems().addAll(ds);
employeeAddDepartment.getItems().addAll(ds);
employeeEditDepartment.getItems().addAll(ds);