Issue with the release of JavaFX 2.0. How to update choiceBox, which presents a list of objects when an object is updated?

I have a Box selection that represents list objects. When a name representing one of these objects is changed by another bit of code, the name in the drop-down list for the selection field does not change. For example, if I have a selection box consisting of a list of Test objects. The code for testing is shown below:

class Test { String name; public Test(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return name; } } 

Then you have the Box selection as follows:

 ChoiceBox<Test> chi = new ChoiceBox<>(); ObservableList<Test> items = FXCollections.observableArrayList(); chi.setItems(items); items.addAll(new Test("ITEM1"),new Test("ITEM2"),new Test("ITEM3")); 

ChoiceBox will show list of ITEM1, ITEM2 and ITEM3

If I then changed the name of one of the elements with the following code:

 items.get(1).setName("CHANGED"); 

ChoiceBox will still show a list of ITEM1, ITEM2 and ITEM3. How can I make selectBox update and display a list of ITEM1, CHANGED and ITEM3?

+6
source share
3 answers

Just for completeness - in fx2, you probably follow the replace approach as indicated in another answer. Since fx8, there is a mechanism to tell the list to listen for changes to its contained elements (a precondition is, of course, that your object has properties and notifies listeners of the change):

 /** changed item to * - use property and notify on change * - not override toString (for visuals, use converter instead) */ class Test { StringProperty name; public Test(String name) { setName(name); } public StringProperty nameProperty() { if (name == null) name = new StringProperty(this, "name"); return name; } public void setName(String name) { nameProperty().set(name); } public String getName() { return nameProperty().get(); } } // use in collection with extractor ObservableList<AlbumFx> items = FXCollections.observableList( e -> new Observable[] {e.nameProperty()} ); items.addAll(...); choiceBox = new ChoiceBox<>(items); // tell the choice how to represent the item StringConverter<Test> converter = new StringConverter<Test>() { @Override public String toString(Test album) { return album != null ? album.getName() : null; } @Override public Test fromString(String string) { return null; } }; choiceBox.setConverter(converter); 
+3
source

I had the same issue in JavaFX 8 (with ComboBox too). I was able to get the same functionality by deleting the item and then adding a new one in the same place.

Example:

This gets the selected item, creates a new item, then calls the replace method:

 Channel selected = channelChoiceBox.getSelectionModel().getSelectedItem(); Channel newChan = new Channel("Example", "Channel"); replaceChannel(newChan, selected); 

This replaces the selected channel with a new one, effectively editing it:

 private void replaceChannel(Channel newChan, Channel oldChan) { int i = channelChoiceBox.getItems().indexOf(oldChan); channelChoiceBox.getItems().remove(oldChan); channelChoiceBox.getItems().add(i, newChan); channelChoiceBox.setValue(newChan); } 

This is not ideal, but does the job.

Disclaimer: I am new to Java and programming in general.

+1
source

Yes. This seems to be a problem with JavaFx. I also came across this. Use a ComboBox instead of a ChoiceBox that will work.

-1
source

All Articles