Home > front end >  Javafx datepicker won't response on first click
Javafx datepicker won't response on first click

Time:06-05

I am working on a project that involves using javafx datepicker to pick a date then do some actions based on the date.

Here is the codes that is related to the datepicker.

public class DatepickerController {
    @FXML private DatePicker datepicker;
    
    @FXML
    public void getHolidays(ActionEvent e) {
        datepicker.setOnAction(event -> {
            LocalDate pickedDate = datepicker.getValue();
            int month = pickedDate.getMonthValue();
            int year = pickedDate.getYear();
            int day = pickedDate.getDayOfMonth();
            
            // If this date has a holiday on it, add the date to the holiday list.
        });
        datepicker.setDayCellFactory(new Callback<>() {
            @Override
            public DateCell call(DatePicker param) {
                return new DateCell() {
                    @Override
                    public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty);
                        // Set cells that have holiday to a different colour
                    }
                };
            }
        });
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="somethingsomething.Controller.DatepickerController">
   <DatePicker fx:id="calendar" layoutX="46.0" layoutY="97.0" onAction="#getHolidays" promptText="Pick a Date" />
</AnchorPane>

However every time I run the code, I get nothing for the first pick, instead I got these error messages.

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.PopupControl.isShowing()" because "<parameter1>" is null
        at javafx.controls/com.sun.javafx.scene.control.behavior.DatePickerBehavior.onAutoHide(DatePickerBehavior.java:59)
        at javafx.controls/javafx.scene.control.skin.ComboBoxPopupControl.lambda$createPopup$6(ComboBoxPopupControl.java:482)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
        at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3602)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3906)
        at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
        at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
        at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
        at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
        at java.base/java.lang.Thread.run(Thread.java:833)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.PopupControl.isShowing()" because "<parameter1>" is null
        at javafx.controls/com.sun.javafx.scene.control.behavior.DatePickerBehavior.onAutoHide(DatePickerBehavior.java:59)
        at javafx.controls/javafx.scene.control.skin.ComboBoxPopupControl.lambda$createPopup$5(ComboBoxPopupControl.java:476)
        at javafx.graphics/javafx.stage.PopupWindow.doAutoHide(PopupWindow.java:862)
        at javafx.graphics/javafx.stage.PopupWindow$PopupEventRedirector.handleAutoHidingEvents(PopupWindow.java:1047)
        at javafx.graphics/javafx.stage.PopupWindow$PopupEventRedirector.handleRedirectedEvent(PopupWindow.java:989)
        at javafx.base/com.sun.javafx.event.EventRedirector.dispatchCapturingEvent(EventRedirector.java:106)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(CompositeEventDispatcher.java:43)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:52)
        at javafx.base/com.sun.javafx.event.EventRedirector.redirectEvent(EventRedirector.java:124)
        at javafx.base/com.sun.javafx.event.EventRedirector.dispatchCapturingEvent(EventRedirector.java:103)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(CompositeEventDispatcher.java:43)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:52)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
        at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
        at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
        at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
        at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
        at java.base/java.lang.Thread.run(Thread.java:833)

I get the first half of the error when I take the first pick. After the first pick, the datepicker calendar won't pop off, unless I click somewhere else, then it disappears, and I get second half of the error. After that, the datepicker will work properly.

CodePudding user response:

You have multiple problems.

  1. The fx:id name is calendar but @FXML name is datepicker, they should be the same.
    • I suggest datePicker to follow standard camel case naming conventions.
  2. You add a new event handler for the date picker action every time the action is triggered. Instead you should either:
    • Add the action handler in the initialize method and NOT in FXML OR
    • Add the action handler in FXML and, in the code for getHolidays, only provide the code which implements the action handler (don’t call setOnAction, the FXML loader will do that for you).
  3. You define the cell factory in the action handler. You should not do that there, you should do that in the initialize method.

That is what I saw from quick review of what you provided. You may have other issues.

  • Related