JavaFXPorts: Is this correct behavior for javafx.scene.control.ComboBox on Android device?

拥有回忆 提交于 2019-12-04 07:00:32

问题


After selecting item in ComboBox, then this selected item is not displayed in ComboBox - only Android device, on desktop is it ok. Compare this two screenshots:


[On desktop when item "Option 2" is selected]

and

[On Android device when item "Option 2" is selected]

I am using JavaFXPorts 8.60.8.


回答1:


Based on this question, and giving that on your bug report you mention you are using a Samsung device, there is a known issue in some Samsung devices where the touch event handling done in JavaFXPorts doesn't work as in the rest of Android devices.

While this is fix on JavaFXPorts, you can try the following workaround:

comboBox.setCellFactory(p -> new ListCell<String>() {

        private String item;
        {
            setOnMousePressed(e -> comboBox.getSelectionModel().select(item));
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty); 
            this.item = item;
            setText(item);
        }

    });

Note I've used a mouse pressed event handler instead of a mouse clicked event handler. Since I can't reproduce it, in my case the mouse click is consumed by the list selection event (as this works properly), but probably in your case you can use either pressed or clicked events.




回答2:


Based on Josés answer I've implemented the following generic Helper function, which might help some of you:

public static <T> void removeSelectionBug(ComboBox<T> comboBox) {
    comboBox.setCellFactory(p -> new ListCell<T>() {
        private T item;
        {
            setOnMousePressed(e -> comboBox.getSelectionModel().select(item));
        }


        @Override
        protected void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            this.item = item;
            if (item != null) {
                setText(item.toString());
            }
        }
    });
}

Btw: I've got this bug on all of my Mobiles(Samsung Note 3, Sony XPERIA Z3 Compact and Nexus 4)



来源:https://stackoverflow.com/questions/40373812/javafxports-is-this-correct-behavior-for-javafx-scene-control-combobox-on-andro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!