java - Trying to make custom control, need to notify when a change was made to the value -


i have datepicker, javafx import. need contain calendar property, why made custom control extends datepicker.

however every time datepicker changed should call property thats why think have use .notify method when onaction event performed. throws java.lang.illegalmonitorstateexception exception.

this code using custom control:

public class datepickercontrol extends datepicker {     private objectproperty<calendar> calendar;      public datepickercontrol() {         super();         setvalue(localdate.now());     }      /**      * value of calendar      *      * @return 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 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();     } } 

and call .notify():

dpagendarange.setonaction(new eventhandler<actionevent>() {      @override     public void handle(actionevent t) {        t.notify();     } }); 

i new javafx apologies if code not structural.

first, notify() doesn't think does. part of low-level concurrency api , concerned waking threads in wait() blocking status. can read on here though it's not trying do.

i think looking have objectproperty<calendar> matches date value in datepicker's valueproperty.

to this, define objectproperty<calendar> in usual way: i.e. create once, , have setcalendar(...) method sets value, getcalendar() method gets value, , calendarproperty() method returns property itself.

to maintain binding between objectproperty<calendar> , valueproperty of datepicker, register listener each 1 , update other when either 1 changes.

(also, note numbering of months different between localdate , calendar.)

so this:

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 "+calendarformatter.format(oldvalue.gettime())+" "+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 "+dateformatter.format(oldvalue)+" "+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();     } } 

a simple test:

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);     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -