首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX ChoiceBox不通过setValue()显示值

JavaFX ChoiceBox不通过setValue()显示值
EN

Stack Overflow用户
提问于 2019-12-13 14:51:56
回答 2查看 394关注 0票数 1

我怀疑我在JavaFX中偶然发现了一个bug。

我有几个TableViews,它们保存有关不同对象的信息。

在本例中,我有一个具有名称的Examiner对象和一个相应的Course对象。

我已经创建了一个函数selectExaminer(),当单击Examiner对象时,它将填充检验者名称TextField和课程ChoiceBox,并将其相应的值。

但是从上面的截图中可以看到,只有TextField examinerName被填充,而ChoiceBox choiceBoxExaminer没有填充。下面是方法:(在initialize()方法中调用它)

代码语言:javascript
复制
  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的测试程序的值时,两者都显示它们已被填充。

代码语言:javascript
复制
      System.out.println("Choice box: " + choiceBoxExaminer.getValue());
      System.out.println("Actual object: " + aux.getCourse());

但是唉..。ChoiceBox仍然是空白的。

这个问题是在将数据存储到二进制文件(这是一个大学项目,没有db)之后出现的,尽管我不确定它如何影响特定的问题。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-13 15:03:36

试试choiceBoxExaminer.getSelectionModel().setSelectedItem(aux.getCourse());

但是,老实说,你提出了一个很好的观点;你会认为setValue()也能做到这一点。

票数 2
EN

Stack Overflow用户

发布于 2021-10-08 05:51:39

代码语言:javascript
复制
aux.getCourse() 

确保返回值与添加到choiceBox的列表中的项完全相同。只有choicebox.setValue()工作:

代码语言:javascript
复制
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. 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59324913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档