java - Handling events from within a PopupWindow -


i've made own timepicker supposed work datepicker. know best way handle event such selecting time , confirming popupwindow.

i could:

  • make timepicker's popup node (a separate fxml , controller) define interface , force timepicker parent implement methods handle selected date. (i'd avoid using interfaces in manner. seems terribly way things.)
  • register kind of custom eventhandler , listener popup window? then, if click okay after selecting date popupwindow, event can fired way timepicker.
  • implement kind of callback-like function. in android, example, there options going screen solely retrieve result. i'm not sure if javafx has kind of thing. screens quite separated each other.

just expose readonlyproperty representing value. user of popup can observe property.

here's proof of concept using datepicker:

import java.time.localdate;  import javafx.application.application; import javafx.beans.property.readonlyobjectproperty; import javafx.beans.property.readonlyobjectwrapper; import javafx.geometry.bounds; import javafx.geometry.insets; import javafx.geometry.point2d; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.datepicker; import javafx.scene.control.label; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.stage.popup; import javafx.stage.popupwindow; import javafx.stage.stage;   public class datepickerpopupexample extends application {      @override     public void start(stage primarystage) {         label datelabel = new label(localdate.now().tostring());         button changebutton = new button("change");          hbox root = new hbox(5, datelabel, changebutton);         root.setalignment(pos.center);          changebutton.setonaction(event -> {             datepickerpopup popup = new datepickerpopup();             popup.valueproperty().addlistener((obs, olddate, newdate) -> {                 datelabel.settext(newdate.tostring());             });             bounds buttonbds = changebutton.getboundsinlocal();             point2d loc = changebutton.localtoscreen(buttonbds.getmaxx(), buttonbds.getminy());             popup.showpopup(primarystage, loc.getx(), loc.gety());         });          scene scene = new scene(root, 250, 150);         primarystage.setscene(scene);         primarystage.show();     }      public class datepickerpopup  {         private final readonlyobjectwrapper<localdate> value = new readonlyobjectwrapper<>();          private final popup popup ;          public readonlyobjectproperty<localdate> valueproperty() {             return value.getreadonlyproperty();         }          public final localdate getvalue() {             return valueproperty().get();         }         public datepickerpopup(localdate date) {              value.set(date);              datepicker picker = new datepicker(date);             button okbutton = new button("ok");             okbutton.setonaction(event -> {                 popup.hide();                 value.set(picker.getvalue());             });             button cancelbutton = new button("cancel");             cancelbutton.setonaction(event -> {                 popup.hide();             });              borderpane root = new borderpane();             root.setcenter(picker);             hbox buttons = new hbox(5, okbutton, cancelbutton);             buttons.setpadding(new insets(5));             buttons.setalignment(pos.center);             root.setbottom(buttons);              popup = new popup();             popup.getcontent().add(root);         }          public datepickerpopup() {             this(localdate.now());         }          public void showpopup(stage owner, double x, double y) {             popup.show(owner, x, y);         }     }      public static void main(string[] args) {         launch(args);     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -