JavaFX从DatePicker中提取日历弹出窗口/仅显示弹出窗口

时间:2016-01-08 16:44:10

标签: java javafx datepicker calendar

我正在尝试为JavaFX应用程序构建CalendarView,仅显示日期(不需要选择)。由于<table> <tr> <td class="circle">1</td> <td class="circle">2</td> <td class="circle">3</td> <td class="circle">4</td> </tr> </table>类有一个很好的日历弹出窗口,我想我可能会尝试提取它,以便我已经涵盖了所有的样式问题。

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

我已经查看了DatePicker类中的show()方法,看看触发弹出窗口时究竟发生了什么,但我不得不承认我无法理解它。

或者我可以考虑简单地编辑DatePicker,只有弹出窗口始终显示编辑器 - TextField和按钮组件始终隐藏,但我再也无法弄清楚如何在不隐藏弹出窗口也是如此。另外,我可能需要让弹出窗口的边界适当地管理这个替代方案中的高度和宽度,这似乎并不那么容易。

2 个答案:

答案 0 :(得分:5)

您可以从DatePickerSkin获取DatePicker的弹出内容。有关实施,请参阅此演示:

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);
    }
}

popup content

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

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);

popup content with hidden top bar

<强>更新

从JDK 9开始,DatePickerSkin是Public API的一部分,不再需要使用已关闭的com.sun.[...]实现。 (见JavaDoc

另外,as mentioned in the comments,要获取所选值,您必须访问从中提取皮肤的DatePicker(例如,将其保存为变量)。

DatePicker datePicker = new DatePicker(LocalDate.now());
DatePickerSkin datePickerSkin = new DatePickerSkin(datePicker);
Node popupContent = datePickerSkin.getPopupContent();
//[...]
LocalDate selectedDate = datePicker.getValue();

您还可以通过向关联的媒体资源添加ChangeListener来收听价值变化:

datePicker.valueProperty().addListener(new ChangeListener<LocalDate>() {
    @Override
    public void changed(ObservableValue<? extends LocalDate> observable, LocalDate oldValue, LocalDate newValue) {
        System.out.println("New Value: " + newValue);
    }
});
//Or using neat lambda
datePicker.valueProperty().addListener((observable, oldValue, newValue) -> {
    System.out.println("New Value: " + newValue);
});

答案 1 :(得分:0)

要删除“月 - 年 - 窗格”,请使用:

%-3s %-26s %-16s %-16s %-20s %-12s %-12s %-16s
Traceback (most recent call last):
File "ec2-instances.py", line 97, in <module>
sys.exit(main())
File "ec2-instances.py", line 78, in main
hosts=list_instances(filter)
File "ec2-instances.py", line 12, in list_instances
print (columns_format) ("num", "Name", "Public IP", "Private IP", "ID", 
"Type", "VPC", "Status")
TypeError: 'NoneType' object is not callable