Run the following snippet
package application;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
final ComboBox<String> myCombo = new ComboBox<>();
final ListCell<String> customCell = new ListCell<String>() {
{
indexProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
System.err.println(getIndex());
}
});
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item);
}
};
myCombo.setButtonCell(customCell);
myCombo.setItems(FXCollections.observableArrayList("A","B","C"));
myCombo.setValue("A");
root.setCenter(myCombo);
Button b = new Button("Update List");
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
myCombo.setItems(FXCollections.observableArrayList("A","B","C","D"));
System.err.println("SelectionModel: " + myCombo.getSelectionModel().getSelectedIndex());
System.err.println("Cell-Index: " + customCell.getIndex());
}
});
root.setBottom(b);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Notice that the cellIndex value still is on 0 although the combo�-value changed to -1. This makes the UI show the original selection although there's none. I think the code needs to set the index to -1 in updateDisplayNode()
package application;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
final ComboBox<String> myCombo = new ComboBox<>();
final ListCell<String> customCell = new ListCell<String>() {
{
indexProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
System.err.println(getIndex());
}
});
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item);
}
};
myCombo.setButtonCell(customCell);
myCombo.setItems(FXCollections.observableArrayList("A","B","C"));
myCombo.setValue("A");
root.setCenter(myCombo);
Button b = new Button("Update List");
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
myCombo.setItems(FXCollections.observableArrayList("A","B","C","D"));
System.err.println("SelectionModel: " + myCombo.getSelectionModel().getSelectedIndex());
System.err.println("Cell-Index: " + customCell.getIndex());
}
});
root.setBottom(b);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Notice that the cellIndex value still is on 0 although the combo�-value changed to -1. This makes the UI show the original selection although there's none. I think the code needs to set the index to -1 in updateDisplayNode()