I have a ListView control in a JavaFX 2 modal dialog.
This ListView displays the DXAlias, ListCells instances for which the factory cell is being produced. The main thing that factory objects do is to look at these UserData ListView properties and compare them with the element corresponding to the ListCell. If they match, the contents of the ListCell are displayed in red; otherwise, in black. I am doing this to indicate which of the items in the ListView is currently selected as "default." Here is my ListCell factory class so you can see what I mean:
private class AliasListCellFactory implements Callback<ListView<DXSynonym>, ListCell<DXSynonym>> { @Override public ListCell<DXSynonym> call(ListView<DXSynonym> p) { return new ListCell<DXSynonym>() { @Override protected void updateItem(DXSynonym item, boolean empty) { super.updateItem(item, empty); if (item != null) { DXSynonym dx = (DXSynonym) lsvAlias.getUserData(); if (dx != null && dx == item) { this.setStyle("-fx-text-fill: crimson;"); } else { this.setStyle("-fx-text-fill: black;"); } this.setText(item.getDxName()); } else { this.setText(Census.FORMAT_TEXT_NULL); } }}; }
I have a button handle "handleAliasDefault ()" that makes the selected item in the ListView new by default, taking the selected DXAlias ​​instance and saving it in the ListView: lsvAlias.setUserData (selected by DXAlias). Here is the handler code:
// Handler for Button[fx:id="btnAliasDefault"] onAction @FXML void handleAliasDefault(ActionEvent event) { int sel = lsvAlias.getSelectionModel().getSelectedIndex(); if (sel >= 0 && sel < lsvAlias.getItems().size()) { lsvAlias.setUserData(lsvAlias.getItems().get(sel)); } }
Since the change made in response to clicking the Set Default button is to change the ListView UserData () without making any changes to the ObservableList database, the list incorrectly indicates the new default.
Is there a way to get ListView to re-render its ListCells? There are four quadrillion Android related issues on this subject, but there seems to be no happiness for JavaFX. I may have to make a “pointless change” to the support array to force a redraw.
I see it was suggested for JavaFX 2.1: Updating the Javafx ListView
source share