我怀疑我在JavaFX中偶然发现了一个bug。
我有几个TableViews,它们保存有关不同对象的信息。

在本例中,我有一个具有名称的Examiner对象和一个相应的Course对象。
我已经创建了一个函数selectExaminer(),当单击Examiner对象时,它将填充检验者名称TextField和课程ChoiceBox,并将其相应的值。
但是从上面的截图中可以看到,只有TextField examinerName被填充,而ChoiceBox choiceBoxExaminer没有填充。下面是方法:(在initialize()方法中调用它)
public void selectExaminer(){
examinerTableView.getSelectionModel().setCellSelectionEnabled(true);
ObservableList selectedCells = examinerTableView.getSelectionModel().getSelectedItems();
selectedCells.addListener(new ListChangeListener() {
@Override
public void onChanged(Change c) {
if(selectedCells.size()>0)
{
Examiner aux = (Examiner) selectedCells.get(0);
examinerName.setText(aux.getName());
choiceBoxExaminer.setValue(aux.getCourse()); //here is the issue
System.out.println("Choice box: " + choiceBoxExaminer.getValue());
System.out.println("Actual object: " + aux.getCourse());
lastExaminerSelectedName = examinerName.getText();
}
}
});ChoiceBox下拉列表确实有效,但不显示通过.setValue()设置的值。

在控制台上打印实际检查程序的Course值和来自TableView的测试程序的值时,两者都显示它们已被填充。
System.out.println("Choice box: " + choiceBoxExaminer.getValue());
System.out.println("Actual object: " + aux.getCourse());

但是唉..。ChoiceBox仍然是空白的。
这个问题是在将数据存储到二进制文件(这是一个大学项目,没有db)之后出现的,尽管我不确定它如何影响特定的问题。
谢谢
发布于 2019-12-13 15:03:36
试试choiceBoxExaminer.getSelectionModel().setSelectedItem(aux.getCourse());
但是,老实说,你提出了一个很好的观点;你会认为setValue()也能做到这一点。
发布于 2021-10-08 05:51:39
aux.getCourse() 确保返回值与添加到choiceBox的列表中的项完全相同。只有choicebox.setValue()工作:
String[] fruits = {"apple", "orange"};
choiceBox.setItems(FXCollections.observableArrayList(fruits);
choiceBox.setValue("grape");//this won't work since grape isn's in the list.
choiceBox.setValue("orange"); // choicebox will display the value. https://stackoverflow.com/questions/59324913
复制相似问题