Values in a ComboBox with a callback that modifies the cell text is not selectable with the mouse. Execute the code below and try to select a value. It does not work with the mouse but it works using the keyboard. Additionally the selected value is displayed using a different string than displayed in the cell, i.e. it does not use the custom cell.
package jfxtest;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class ComboTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane pane = new FlowPane();
final ComboBox<Integer> combo = new ComboBox<>();
combo.getItems().addAll(1, 2, 3);
combo.setCellFactory(new Callback<ListView<Integer>, ListCell<Integer>>() {
@Override
public ListCell<Integer> call(ListView<Integer> param) {
return new ListCell<Integer>(){
@Override
protected void updateItem(Integer item, boolean empty) {
setText("#" + item);
}
};
}
});
pane.getChildren().add(combo);
final Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle(getClass().getSimpleName());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package jfxtest;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class ComboTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane pane = new FlowPane();
final ComboBox<Integer> combo = new ComboBox<>();
combo.getItems().addAll(1, 2, 3);
combo.setCellFactory(new Callback<ListView<Integer>, ListCell<Integer>>() {
@Override
public ListCell<Integer> call(ListView<Integer> param) {
return new ListCell<Integer>(){
@Override
protected void updateItem(Integer item, boolean empty) {
setText("#" + item);
}
};
}
});
pane.getChildren().add(combo);
final Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle(getClass().getSimpleName());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}