简体   繁体   English

JavaFX ChoiceBox 不通过 setValue() 显示值

[英]JavaFX ChoiceBox does not display value through setValue()

I have a suspicion that I have stumbled upon a bug in JavaFX.我怀疑我偶然发现了 JavaFX 中的一个错误。

I have several TableViews that hold information about different objects.我有几个TableViews保存有关不同对象的信息。

在此处输入图片说明

In this example, I have an Examiner object with a name and a corresponding Course object.在此示例中,我有一个带有名称和相应Course对象的Examiner对象。

I have created a function selectExaminer() that populates the Examiner name TextField and the Course ChoiceBox upon clicking on the Examiner object from the TableView with it's corresponding values.我创建了一个函数selectExaminer() ,在点击TableViewExaminer对象时,它会填充 Examiner name TextField和 Course ChoiceBox相应的值。

But as can be seen from the screenshot above, only the TextField examinerName is populated, while the ChoiceBox choiceBoxExaminer is not.但是从上面的屏幕截图中可以看出,只有TextField examinerName ChoiceBox choiceBoxExaminer被填充,而ChoiceBox choiceBoxExaminer没有被填充。 Here is the method: (it is called in the initialize() method)这是方法:(它在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();
    }
  }
});

The ChoiceBox dropdown does work but doesn't display the value set through .setValue() ChoiceBox下拉菜单确实有效,但不显示通过 .setValue() 设置的值

在此处输入图片说明

When printing to the console the value of the Course of the actual Examiner and the one from the TableView , both show that they are populated.当向控制台打印实际考官的Course值和来自TableViewCourse值时,都显示它们已填充。

      System.out.println("Choice box: " + choiceBoxExaminer.getValue());
      System.out.println("Actual object: " + aux.getCourse());

在此处输入图片说明

But alas... the ChoiceBox is still blank.但唉...... ChoiceBox仍然是空白的。

This issue arose after implementing data storage to binary files (this is a college project, no db), although I'm not sure how it influences the particular issue这个问题是在将数据存储到二进制文件之后出现的(这是一个大学项目,没有数据库),尽管我不确定它是如何影响特定问题的

Thank you谢谢

I have a suspicion that I have stumbled upon a bug in JavaFX.我怀疑我偶然发现了JavaFX中的错误。

I have several TableViews that hold information about different objects.我有几个TableViews包含有关不同对象的信息。

在此处输入图片说明

In this example, I have an Examiner object with a name and a corresponding Course object.在这个例子中,我有一个带有名称的Examiner对象和一个对应的Course对象。

I have created a function selectExaminer() that populates the Examiner name TextField and the Course ChoiceBox upon clicking on the Examiner object from the TableView with it's corresponding values.我已经创建了一个函数selectExaminer() ,当从TableView单击带有相应值的Examiner对象时,该函数将填充Examiner名称TextField和Course ChoiceBox

But as can be seen from the screenshot above, only the TextField examinerName is populated, while the ChoiceBox choiceBoxExaminer is not.但是从上面的屏幕截图可以看出,只有TextField examinerName ChoiceBox choiceBoxExaminer被填充,而ChoiceBox choiceBoxExaminer没有被填充。 Here is the method: (it is called in the initialize() method)这是方法:(在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();
    }
  }
});

The ChoiceBox dropdown does work but doesn't display the value set through .setValue() ChoiceBox下拉列表可以工作,但不会显示通过.setValue()设置的值

在此处输入图片说明

When printing to the console the value of the Course of the actual Examiner and the one from the TableView , both show that they are populated.在控制台上打印实际检查员Course的值和TableView ,都表明它们已被填充。

      System.out.println("Choice box: " + choiceBoxExaminer.getValue());
      System.out.println("Actual object: " + aux.getCourse());

在此处输入图片说明

But alas... the ChoiceBox is still blank.但是可惜... ChoiceBox仍然是空白。

This issue arose after implementing data storage to binary files (this is a college project, no db), although I'm not sure how it influences the particular issue在将数据存储到二进制文件后实现了此问题(这是一个大学项目,没有数据库),尽管我不确定它如何影响特定问题

Thank you谢谢

aux.getCourse() 

Make sure that the return value is exactly the same as an item in the list added to the choiceBox.确保返回值与添加到choiceBox 的列表中的项目完全相同。 Only choicebox.setValue() works:只有 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. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM