3

I noticed a strange behavior when selecting a value from a ComboBox from the initialize method annotated with @FXML. The behavior is that on initial selection from the control's popup sheet, it is not hidden.

ComboBox

Here is an example project

public class Main  extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Button test = new Button("Test");
        test.setOnAction(event -> {
            TestDialog dialog = new TestDialog(stage);
            dialog.show();
        });

        HBox hBox = new HBox(test);
        stage.setScene(new Scene(hBox, 300, 50));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public class TestDialog extends Dialog<ButtonType> {

    @FXML
    private ComboBox<Integer> baudRateComboBox;

    public TestDialog(Window owner) {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/com/sample/dialogpane.fxml"));
            loader.setController(this);

            initOwner(owner);
            initModality(Modality.APPLICATION_MODAL);

            setDialogPane(loader.load());
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @FXML
    private void initialize() {
        baudRateComboBox.getItems().setAll(4800, 9600, 19200, 38400, 57600, 115200);

        baudRateComboBox.getSelectionModel().select((Integer) 115200);
    }
}
<DialogPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">

    <content>
        <HBox spacing="5" alignment="BASELINE_LEFT">
            <Label text="Baudrate:"/>
            <ComboBox fx:id="baudRateComboBox"/>
        </HBox>
    </content>

    <buttonTypes>
        <ButtonType text="Затвори" buttonData="CANCEL_CLOSE"/>
    </buttonTypes>

</DialogPane>

If I select the value asynchronously, the problem disappears.

@FXML
private void initialize() {
    baudRateComboBox.getItems().setAll(4800, 9600, 19200, 38400, 57600, 115200);

    Platform.runLater(() -> baudRateComboBox.getSelectionModel().select((Integer) 115200));
}

I am using jdk 18.0.1.1 on Ubuntu and javafx 19

mr mcwolf
  • 2,574
  • 2
  • 14
  • 27
  • 1
    This looks like a bug. I haven’t had a chance to test it or check if it’s been reported, but you should consider reporting it if it’s not already known. – James_D Dec 13 '22 at 11:43
  • 3
    it's a bug fixed for fx20 .. don't have it handy, but a recent duplicate (that I don't find right now .. grrrI) has a reference – kleopatra Dec 13 '22 at 11:55
  • 4
    okay, found the duplicate [Issue with ComboBox focus in FX 19](https://stackoverflow.com/questions/74147187/issue-with-combobox-focus-in-fx-19) - the fixed issue is https://bugs.openjdk.org/browse/JDK-8297130, you might check if it also fixes this – kleopatra Dec 13 '22 at 11:59
  • 3
    I downloaded and tested with javafx `20-ea+11` everything works, both the test project and the real one where I noticed the problem. 10x @kleopatra – mr mcwolf Dec 13 '22 at 12:28

0 Answers0