Display Name Display Name - 3 months ago 47
Java Question

Add a DateTimePicker to a Button

I am trying to implement a

CalendarPicker
so that it will open in a new window when i press a
Button
in JavaFX

I have added some code for my calendar:

@FXML
public void dateTimePicker(Stage primaryStage){
CalendarPicker dateTime = new CalendarPicker();
dateTime.withCalendar(Calendar.getInstance());
dateTime.withShowTime(Boolean.TRUE);
dateTime.withLocale(Locale.ENGLISH);
dateTime.calendarProperty().addListener(new ChangeListener<Calendar>() {

public void changed(ObservableValue<? extends Calendar> ov, Calendar t, Calendar t1) {
System.out.println("Selected date: "+t1.getTime().toString());
}
});
StackPane root = new StackPane();
root.getChildren().add(dateTime);

Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Date & Time from JFXtras 2.2");
primaryStage.setScene(scene);
primaryStage.show();
}


Then added a button to my FXML

<Button id="dateTimePicker" fx:id="dateTimePicker" layoutX="14.0" layoutY="398.0" mnemonicParsing="false" text="Start" />


and finally initialised my
Button


@FXML
private Button dateTimePicker,


My button doesn't actually do anything- no new windows open. I don't get any errors but I do get a warning in Eclipse

Parameter 'primaryStage' is not assignable to javafx.event.Event


What have I done wrong?

EDIT

Adding
onAction
attribute to button has stopped my application running. The error is:

Error resolving onAction='#dateTimePicker', either the event handler is not in the Namespace or there is an error in the script.

Answer

Assuming you have a controller set up for the FXML file, and the dateTimePicker() method is defined in that controller, e.g. as in the documentation example the following will work.

First, you need to map the action for the button to the method you defined using the onAction property of the button:

<Button id="dateTimePicker" fx:id="dateTimePicker" onAction="#dateTimePicker" layoutX="14.0" layoutY="398.0" mnemonicParsing="false" text="Start" />

The handler method either needs to take a parameter matching the type of event that is generated (ActionEvent for onAction), or no parameters. This is the cause of the warning you're getting in Eclipse. Since you don't need to access the event itself, just define the method to take no parameters:

@FXML
public void dateTimePicker(){

    // ...
}

Finally, if you want to show a new window, you have to create a new window. So:

@FXML
public void dateTimePicker(){
    CalendarPicker dateTime = new CalendarPicker();
    dateTime.withCalendar(Calendar.getInstance());
    dateTime.withShowTime(Boolean.TRUE);
    dateTime.withLocale(Locale.ENGLISH);
    dateTime.calendarProperty().addListener(new ChangeListener<Calendar>() {

        public void changed(ObservableValue<? extends Calendar> ov, Calendar t, Calendar t1) {
            System.out.println("Selected date: "+t1.getTime().toString());
        }
    });
    StackPane root = new StackPane();
    root.getChildren().add(dateTime);

    Scene scene = new Scene(root, 300, 250);

    Stage stage = new Stage();

    stage.setTitle("Date & Time from JFXtras 2.2");
    stage.setScene(scene);
    stage.show();
}
Comments