Tuesday, August 5, 2014

JavaFX 8 example: Preset/load DataPicker at now


package javafx_datepicker;

import java.time.LocalDate;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_DatePicker extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        //New DataPicker init at now
        DatePicker datePicker = new DatePicker(LocalDate.now());
        datePicker.setOnAction(new EventHandler() {

            @Override
            public void handle(Event event) {
                LocalDate date = datePicker.getValue();
                System.err.println("Selected date: " + date);
            }

        });

        //reload datePicker at now
        Button btnNow = new Button("Now");
        btnNow.setOnAction(new EventHandler() {

            @Override
            public void handle(Event event) {
                datePicker.setValue(LocalDate.now());
            }

        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(btnNow, datePicker);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);

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

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

No comments:

Post a Comment