JavaFX从DatePicker中提取日历弹出窗口仅显示弹出窗口 [英] JavaFX extract calendar-popup from DatePicker / only show popup

查看:207
本文介绍了JavaFX从DatePicker中提取日历弹出窗口仅显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为JavaFX应用程序构建一个CalendarView,只显示日期(不需要选择)。因为 DatePicker 类有一个漂亮的日历弹出窗口,我以为我可能会尝试提取它,以便我已经覆盖了所有的样式问题。

I'm trying to build an CalendarView for an JavaFX application, to only display dates(no selecting required). Since the DatePicker class has a nice calendar popup I thought I might try to extract it so that I already have all the style-questions covered.

所以有一个简单的方法来提取DatePicker日历弹出窗口并将其插入到新的CalendarView?

So is there an easy way to extract the DatePicker calendar popup and insert it into a new CalendarView?

我已经看过 show()方法从 ComboBoxBase 类来查看当触发弹出窗口时会发生什么,但是我不得不承认我无法真的让我的头晕了。

I already looked at the show() method from the ComboBoxBase class to see what exactly happens when the popup is triggered but I have to admit I couldn't really get my head around it.

或者,我可以考虑简单地编辑DatePicker,只有弹出窗口一直显示编辑器 - TextField和按钮组件总是隐藏起来,但是我再也无法弄清楚如何在没有隐藏弹出窗口的情况下做到这一点。另外我也可能需要获得弹出窗口的边界,以便在这个替代方案中轻松管理高度和宽度,这似乎并不那么容易。

Alternatively I could think about simply edit the DatePicker in a way that only the popup shows all the time with the editor-TextField and button component always hiding, but again I couldn't quite figure out how to do that without hiding the popup as well. Also I would propably need to get the bounds of the popup to approptiately manage height and width in this alternativw, which again seems to be not that easy.

推荐答案

您可以从DatePickerSkin获取DatePicker的弹出内容。看到这个演示实现:

You can get the popup content of a DatePicker from a DatePickerSkin. See this demo for an implementation:

public class DatePickerPopupDemo extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            DatePickerSkin datePickerSkin = new DatePickerSkin(new DatePicker(LocalDate.now()));
            Node popupContent = datePickerSkin.getPopupContent();

            root.setCenter(popupContent);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

如果不需要顶栏,可以查找并隐藏。

If the top bar is not needed, you can look it up and hide it.

DatePickerSkin datePickerSkin = new DatePickerSkin(new DatePicker(LocalDate.now()));
Node popupContent = datePickerSkin.getPopupContent();

// force a css layout pass to ensure that lookup calls work
popupContent.applyCss();
popupContent.lookup(".month-year-pane").setVisible(false);

root.setCenter(popupContent);

这篇关于JavaFX从DatePicker中提取日历弹出窗口仅显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆