Silent_Jager
Silent_Jager

Reputation: 111

JavaFX ChoiceBox does not display value through setValue()

I have a suspicion that I have stumbled upon a bug in JavaFX.

I have several TableViews that hold information about different objects.

enter image description here

In this example, I have an Examiner object with a name and a corresponding Course object.

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.

But as can be seen from the screenshot above, only the TextField examinerName is populated, while the ChoiceBox choiceBoxExaminer is not. Here is the method: (it is called in the initialize() method)

  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()

enter image description here

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.

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

enter image description here

But alas... the ChoiceBox is still blank.

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

Upvotes: 1

Views: 691

Answers (2)

Larry
Larry

Reputation: 1

aux.getCourse() 

Make sure that the return value is exactly the same as an item in the list added to the choiceBox. Only choicebox.setValue() works:

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. 

Upvotes: 0

danielehmig
danielehmig

Reputation: 116

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

But, honestly, you make a good point; you would think that setValue() would also do the trick.

Upvotes: 2

Related Questions