Issue
I have a suspicion that I have stumbled upon a bug in JavaFX.
I have several TableViews
that hold information about different objects.
src="https://i.stack.imgur.com/FT45S.png" alt="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()
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());
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
Solution
Try choiceBoxExaminer.getSelectionModel().setSelectedItem(aux.getCourse());
But, honestly, you make a good point; you would think that setValue()
would also do the trick.
Answered By - danielehmig
Answer Checked By - Timothy Miller (JavaFixing Admin)