Wednesday, August 6, 2014

DataPicker.setChronology(), set calendar system for DatePicker

The method DataPicker.setChronology(java.time.chrono.Chronology value) set calendar system for DatePicker. Available Chronology value can be found here: http://docs.oracle.com/javase/8/docs/api/java/time/chrono/AbstractChronology.html.


package javafx_datepicker;

import java.time.LocalDate;
import java.time.chrono.Chronology;
import java.time.chrono.HijrahChronology;
import java.time.chrono.IsoChronology;
import java.time.chrono.JapaneseChronology;
import java.time.chrono.MinguoChronology;
import java.time.chrono.ThaiBuddhistChronology;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
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());
            }

        });
        
        final ToggleGroup groupChronology = new ToggleGroup();
        RadioButton optDefault = new RadioButton("default");
        optDefault.setToggleGroup(groupChronology);
        optDefault.setSelected(true);
        optDefault.setUserData(null);
        
        RadioButton optHijrah = new RadioButton("HijrahChronology");
        optHijrah.setToggleGroup(groupChronology);
        optHijrah.setUserData(HijrahChronology.INSTANCE);
        
        RadioButton optIso = new RadioButton("IsoChronology");
        optIso.setToggleGroup(groupChronology);
        optIso.setUserData(IsoChronology.INSTANCE);
        
        RadioButton optJapanese = new RadioButton("JapaneseChronology");
        optJapanese.setToggleGroup(groupChronology);
        optJapanese.setUserData(JapaneseChronology.INSTANCE);
        
        RadioButton optMinguo = new RadioButton("MinguoChronology");
        optMinguo.setToggleGroup(groupChronology);
        optMinguo.setUserData(MinguoChronology.INSTANCE);
        
        RadioButton optThaiBuddhist = new RadioButton("ThaiBuddhistChronology");
        optThaiBuddhist.setToggleGroup(groupChronology);
        optThaiBuddhist.setUserData(ThaiBuddhistChronology.INSTANCE);
        
        groupChronology.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){

            @Override
            public void changed(ObservableValue<? extends Toggle> observable, 
                    Toggle oldValue, Toggle newValue) {
                if (groupChronology.getSelectedToggle() != null) {
                    datePicker.setChronology(
                        (Chronology)groupChronology.getSelectedToggle().getUserData());
                
                }else{
                    datePicker.setChronology(null);
                }
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(optDefault, 
            optHijrah, optIso, optJapanese, optMinguo, optThaiBuddhist,
            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