How to disable hiding of combobox popup in JavaFX8

2019-06-02 04:51发布

Is there a way to constantly show combobox popup? The question was about datepicker, but it is a descendant of combobox. I want to call show() method of combobox and then constantly show it until stage is closed. The best thing that it got till now is

    showingProperty().addListener({ ov, old, newValue ->
        if (!newValue) this.show()
    })

It kinda works, but it hides popup and then shows it, and that is inconvinient.

2条回答
Emotional °昔
2楼-- · 2019-06-02 05:48

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.

enter image description here

http://jfxtras.org/

查看更多
混吃等死
3楼-- · 2019-06-02 05:51

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

查看更多
登录 后发表回答