Reputation: 9969
I am encountering some strange issues after upgrading to latest JavaFX version (19).
If I set the last option as the value in ComboBox, then on first opening, the dropdown will not hide if I select any option. After that, the dropdown works as usual. The strange part is this only happens if I set the last option as value. When I set any other option apart from last option, it is working well.
In the above example, you can notice that:
When I tried to investigate the root cause, I am almost concluded that it is because of the new feature "focusWithIn". Because that is where it is causing the issue to happen.
But on further investigation, I also noticed that the focus got messed up. In the above gif, you can also notice that the "focused" style is not applied on ComboBox if I set last option as value (again only for first time till I moved focus to another node and when I am back, it works as usual). Whereas the focused style works well if I set a different value.
When I tried to put some logs on focused property, below is the output when I focus on ComboBox (with last option as value):
ComboBox focused : true
ComboBox focused : false
The focus is immediately turned off!!
The only thing that confuses me is "Why only with last option??". I know issues/bugs will occur in general. But this particual relation with last option, I couldn't get it :)
Anyway, I tried different ways to make the dropdown hiding and focus to work, but none worked. Do any of you have any suggestions(workaround) to let this fix.
Below is the working demo:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ComboBoxDemo extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("One", "Two", "Three", "Four");
comboBox.getSelectionModel().select("Four"); // Change value to any other options, it works
comboBox.focusedProperty().addListener((obs, old, focused) -> {
System.out.println("ComboBox focused : " + focused+", showing: "+comboBox.isShowing());
// ATTEMPT #2 : Requesting the focus by conditional checking (DIDN'T WORKED)
if(comboBox.isShowing() && !focused){
comboBox.requestFocus();
}
});
// ATTEMPT #1 : Requesting the focus after the dropdown is shown (DIDN'T WORKED)
comboBox.setOnShown(e -> {
System.out.println("ComboBox shown...");
comboBox.requestFocus();
});
VBox root = new VBox(new CheckBox("Just for focus grabbing"), comboBox);
root.setAlignment(Pos.CENTER);
root.setSpacing(20);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("ComboBox FX " + System.getProperty("javafx.runtime.version"));
primaryStage.setScene(scene);
primaryStage.show();
}
}
Upvotes: 5
Views: 312
Reputation: 378
Definitely a bug in JavaFX 19. I tested a bunch of things, and it seems that in order to set an initial selection for a ComboBox at the time of generation, it requires two ticks:
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("One", "Two", "Three", "Four");
initializeComboBox(comboBox, 3);
private void initializeComboBox(ComboBox comboBox, int intialIndex) {
Platform.runLater(()->{
Platform.runLater(()->{
comboBox.getSelectionModel().select(intialIndex);
});
});
}
I cannot answer why it is required for the last item only. That will be a job for the developers. However, this will work as a workaround in the meantime for this particular use case in this version.
Upvotes: 2