Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DatePickerControl extends DatePicker
- {
- private ObjectProperty<Calendar> calendar;
- public DatePickerControl() {
- super();
- setValue(LocalDate.now());
- }
- /**
- * Get the value of calendar
- *
- * @return the value of calendar
- */
- public ObjectProperty<Calendar> calendarProperty() {
- Calendar calendar = new GregorianCalendar();
- System.out.println("test");
- calendar.set(getValue().getYear(), getValue().getMonthValue(), getValue().getDayOfMonth());
- return new SimpleObjectProperty<>(calendar);
- }
- /**
- * Set the value of calendar
- *
- * @param calendar new value of calendar
- */
- public void setCalendar(Calendar calendar) {
- this.calendar.set(calendar);
- LocalDate ld = LocalDate.now();
- ld.withYear(calendar.get(Calendar.YEAR));
- ld.withMonth(calendar.get(Calendar.MONTH));
- ld.withDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
- setValue(ld);
- }
- public Calendar getCalendar() {
- return calendar.get();
- }
- }
- dpAgendaRange.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent t) {
- t.notify();
- }
- });
- public class DatePickerControl extends DatePicker {
- private ObjectProperty<Calendar> calendar;
- private DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_DATE ;
- private Format calendarFormatter = DateFormat.getDateInstance();
- public DatePickerControl() {
- super();
- setValue(LocalDate.now());
- calendar = new SimpleObjectProperty<Calendar>(Calendar.getInstance());
- calendar.addListener((obs, oldValue, newValue) -> {
- System.out.println("calendar changed from "+calendarFormatter.format(oldValue.getTime())+" to "+calendarFormatter.format(newValue.getTime()));
- LocalDate localDate = LocalDate.now()
- .withYear(newValue.get(Calendar.YEAR))
- .withMonth(newValue.get(Calendar.MONTH)+1)
- .withDayOfMonth(newValue.get(Calendar.DAY_OF_MONTH));
- setValue(localDate);
- });
- valueProperty().addListener((obs, oldValue, newValue) -> {
- System.out.println("Value changed from "+dateFormatter.format(oldValue)+" to "+dateFormatter.format(newValue));
- Calendar cal = Calendar.getInstance();
- cal.set(getValue().getYear(), getValue().getMonthValue()-1, getValue().getDayOfMonth());
- calendar.set(cal);
- });
- }
- public ObjectProperty<Calendar> calendarProperty() {
- return calendar;
- }
- public void setCalendar(Calendar calendar) {
- this.calendar.set(calendar);
- }
- public Calendar getCalendar() {
- return calendar.get();
- }
- }
- import java.text.DateFormat;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.control.Label;
- import javafx.scene.layout.VBox;
- public class Main extends Application {
- @Override
- public void start(Stage primaryStage) {
- try {
- VBox root = new VBox();
- Scene scene = new Scene(root,400,400);
- DatePickerControl datePicker = new DatePickerControl();
- Label label = new Label();
- final DateFormat calFormatter = DateFormat.getDateInstance() ;
- datePicker.calendarProperty().addListener((obs, oldValue, newValue) -> label.setText(calFormatter.format(newValue.getTime())));
- root.getChildren().addAll(datePicker, label);
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement