I have a DatePicker and want to add a new MenuItem to ContextMenu of it.
DatePicker inputDate = new DatePicker();
try{
MenuItem clearInput = new MenuItem("Clear");
clearInput.setOnAction(e -> {
inputDate.getEditor().clear();
inputDate.setValue(null);
});
inputDate.getContextMenu() //returns null
.getItems()
.add(0, clearInput);
}catch(Exception e){
e.printStackTrace();
}
At this code I get NullPointerException at .getItems(), which means inputDate.getContextMenu() is null.
Why getContextMenu() returns null?
And how to fix it?
Or how to add a new MenuItem to ContextMenu of DatePicker?