How to disable hiding of combobox popup in JavaFX8?

半城伤御伤魂 提交于 2019-12-06 15:29:16

The bad solution

Take the popup content out of the date picker skin and use it like any other node. Note that the date picker itself must have been rendered as part of the scene at least once for the skin to have been initialized. There may be a more clever way to initialize the skin.

final DatePicker datePicker = new DatePicker();
final StackPane root = new StackPane( datePicker );
final Scene scene = new Scene( root, 250, 200 );
primaryStage.setScene( scene );
primaryStage.show();

datePicker.setVisible( false );
datePicker.setManaged( false );

final com.sun.javafx.scene.control.skin.DatePickerSkin skin = (com.sun.javafx.scene.control.skin.DatePickerSkin) datePicker.getSkin();
root.getChildren().add( skin.getPopupContent() );

Full example code at github.

The good solution

Use a control made specificly for your purpose, like CalendarPicker from JFXtras.

http://jfxtras.org/

If you could override the hide() method of the ComboBoxBase method, you would be able to prevent the control from closing. You would have to make a new class, like alwaysOpenDatePicker and let it extend the javafx scene datapicker class. In that class you could override the hide() method, in which you would do nothing.

I'm not sure if this would work, I'm just thinking out loud. I guess it's worth a try, let me know if it worked :).

And a link to the ComboBoxBase page: https://docs.oracle.com/javafx/2/api/javafx/scene/control/ComboBoxBase.html

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