Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. public class DatePickerControl extends DatePicker
  2. {
  3. private ObjectProperty<Calendar> calendar;
  4.  
  5. public DatePickerControl() {
  6. super();
  7. setValue(LocalDate.now());
  8. }
  9.  
  10. /**
  11. * Get the value of calendar
  12. *
  13. * @return the value of calendar
  14. */
  15. public ObjectProperty<Calendar> calendarProperty() {
  16. Calendar calendar = new GregorianCalendar();
  17. System.out.println("test");
  18. calendar.set(getValue().getYear(), getValue().getMonthValue(), getValue().getDayOfMonth());
  19. return new SimpleObjectProperty<>(calendar);
  20. }
  21.  
  22. /**
  23. * Set the value of calendar
  24. *
  25. * @param calendar new value of calendar
  26. */
  27. public void setCalendar(Calendar calendar) {
  28. this.calendar.set(calendar);
  29. LocalDate ld = LocalDate.now();
  30. ld.withYear(calendar.get(Calendar.YEAR));
  31. ld.withMonth(calendar.get(Calendar.MONTH));
  32. ld.withDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
  33. setValue(ld);
  34. }
  35.  
  36. public Calendar getCalendar() {
  37. return calendar.get();
  38. }
  39. }
  40.  
  41. dpAgendaRange.setOnAction(new EventHandler<ActionEvent>() {
  42.  
  43. @Override
  44. public void handle(ActionEvent t) {
  45. t.notify();
  46. }
  47. });
  48.  
  49. public class DatePickerControl extends DatePicker {
  50. private ObjectProperty<Calendar> calendar;
  51.  
  52. private DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_DATE ;
  53. private Format calendarFormatter = DateFormat.getDateInstance();
  54.  
  55. public DatePickerControl() {
  56. super();
  57. setValue(LocalDate.now());
  58. calendar = new SimpleObjectProperty<Calendar>(Calendar.getInstance());
  59.  
  60. calendar.addListener((obs, oldValue, newValue) -> {
  61. System.out.println("calendar changed from "+calendarFormatter.format(oldValue.getTime())+" to "+calendarFormatter.format(newValue.getTime()));
  62. LocalDate localDate = LocalDate.now()
  63. .withYear(newValue.get(Calendar.YEAR))
  64. .withMonth(newValue.get(Calendar.MONTH)+1)
  65. .withDayOfMonth(newValue.get(Calendar.DAY_OF_MONTH));
  66. setValue(localDate);
  67. });
  68.  
  69. valueProperty().addListener((obs, oldValue, newValue) -> {
  70. System.out.println("Value changed from "+dateFormatter.format(oldValue)+" to "+dateFormatter.format(newValue));
  71. Calendar cal = Calendar.getInstance();
  72. cal.set(getValue().getYear(), getValue().getMonthValue()-1, getValue().getDayOfMonth());
  73. calendar.set(cal);
  74. });
  75. }
  76.  
  77.  
  78. public ObjectProperty<Calendar> calendarProperty() {
  79. return calendar;
  80. }
  81.  
  82. public void setCalendar(Calendar calendar) {
  83. this.calendar.set(calendar);
  84. }
  85.  
  86. public Calendar getCalendar() {
  87. return calendar.get();
  88. }
  89. }
  90.  
  91. import java.text.DateFormat;
  92.  
  93. import javafx.application.Application;
  94. import javafx.stage.Stage;
  95. import javafx.scene.Scene;
  96. import javafx.scene.control.Label;
  97. import javafx.scene.layout.VBox;
  98.  
  99.  
  100. public class Main extends Application {
  101. @Override
  102. public void start(Stage primaryStage) {
  103. try {
  104. VBox root = new VBox();
  105. Scene scene = new Scene(root,400,400);
  106.  
  107. DatePickerControl datePicker = new DatePickerControl();
  108. Label label = new Label();
  109. final DateFormat calFormatter = DateFormat.getDateInstance() ;
  110. datePicker.calendarProperty().addListener((obs, oldValue, newValue) -> label.setText(calFormatter.format(newValue.getTime())));
  111. root.getChildren().addAll(datePicker, label);
  112. primaryStage.setScene(scene);
  113. primaryStage.show();
  114. } catch(Exception e) {
  115. e.printStackTrace();
  116. }
  117. }
  118.  
  119. public static void main(String[] args) {
  120. launch(args);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement