問題描述
我正在嘗試為 JavaFX 應用程序構建 CalendarView,僅顯示日期(無需選擇).由于 DatePicker
類有一個不錯的日歷彈出窗口,我想我可能會嘗試提取它,以便我已經(jīng)涵蓋了所有樣式問題.
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?
我已經(jīng)查看了 ComboBoxBase
類中的 show()
方法,以了解觸??發(fā)彈出窗口時究竟發(fā)生了什么,但我不得不承認我真的不能想一想.
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 以一種只有彈出窗口始終顯示的方式,而 editor-TextField 和按鈕組件總是隱藏,但我還是不知道如何在不隱藏的情況下做到這一點彈出窗口也是如此.此外,我可能需要獲得彈出窗口的邊界以在此替代方法中適當?shù)毓芾砀叨群蛯挾龋@似乎又不是那么容易.
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 的彈出內(nèi)容.有關實現(xiàn),請參閱此演示:
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);
更新:
從 JDK 9 開始,DatePickerSkin
是公共 API 的一部分,不再需要使用封閉的 com.sun.[...]
實現(xiàn).(參見 JavaDoc)
As of JDK 9 DatePickerSkin
is part of the Public API and using the closed com.sun.[...]
implementation is no longer needed. (See JavaDoc)
另外,如評論中所述,要獲取所選值,您必須訪問從中提取皮膚的 DatePicker
(例如,將其保存為變量).
Also, as mentioned in the comments, to get the selected value you have to access the DatePicker
from which you extracted the skin (by saving it as an variable for example).
DatePicker datePicker = new DatePicker(LocalDate.now());
DatePickerSkin datePickerSkin = new DatePickerSkin(datePicker);
Node popupContent = datePickerSkin.getPopupContent();
//[...]
LocalDate selectedDate = datePicker.getValue();
您還可以通過向關聯(lián)屬性添加 ChangeListener
來監(jiān)聽值更改:
You can also listen to value-changes by adding a ChangeListener
to the associated property:
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);
});
這篇關于JavaFX 從 DatePicker 中提取日歷彈出窗口/僅顯示彈出窗口的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!