javafx prevent Enter key propogating from default button action -
i have treeview onkeyrelease(...) handler watches enter key.
it launches modal dialog window has 'ok' button set default onaction(...) handler.
when press enter in dialog onaction() handler invoked, it's work, , and explicitly close() dialog.
the enter key propagated calling treeview , picked again onkeyrelease() handler.
question: how can prevent enter key propagating after button's onaction called? button's default onaction not key event there seems consume it. ideas?
below skeletal representation of doing...
/** * here treeview impl framework... */ public class taxonomyeditor extends ... { private final keyreleaseeventhandler keyreleaseeventhandler = new keyreleaseeventhandler(); private boolean edited; public final void init(taxonomy taxonomy) { ... addeventhandler(appevent.tree_item_added_event, new eventhandler<appevent>() { @override public void handle(appevent event) { // resync ... event.consume(); } }); addeventhandler(appevent.tree_item_changed_event, new eventhandler<appevent>() { @override public void handle(appevent event) { // resync ... event.consume(); } }); addeventhandler(appevent.tree_item_removed_event, new eventhandler<appevent>() { @override public void handle(appevent event) { // resync ... event.consume(); } }); getselectionmodel().getselecteditems().addlistener(new listchangelistener() { @override public void onchanged(listchangelistener.change c) { if (!c.getlist().isempty()) { // selection tracking // ... // reinstall handler after change (see bug note in handler) setonkeyreleased(keyreleaseeventhandler); } } }); } private void newtaxon() { logger.debug("add taxon [{}]", currtaxatreeitem.gettext()); ... boolean edited = new taxondefdialog(currtaxatreeitem, taxondata).edit(); if (edited) { // -------------------------------------------------- // calls service heavy lifting // -------------------------------------------------- taxonomymodel.addtaxon(parenttaxatreeitem, taxondata); } } private void edittaxon() { logger.debug("edit taxon [{}]", currtaxatreeitem.gettext()); ... boolean edited = new taxondefdialog(parenttaxatreeitem, newtaxondata).edit(); if (edited) { // -------------------------------------------------- // service call ... // -------------------------------------------------- taxonomymodel.changetaxon(parenttaxatreeitem, oldtaxondata, newtaxondata); } } private void deletetaxon() { logger.debug("delete taxon [{}]", currtaxatreeitem.gettext()); ... if (confirmdelete()) { // -------------------------------------------------- // service call ... // -------------------------------------------------- taxonomymodel.removetaxon(parenttaxatreeitem, (taxondata) currtaxatreeitem); } } private class keyreleaseeventhandler implements eventhandler<keyevent> { @override public void handle(keyevent event) { logger.debug("keyevent [{}]", event); switch (event.getcode()) { case enter: /*------------------------------------------------------------------*/ /* disable handler before dialog launch solve issue */ /* enter key dialog can not consumed when */ /* fired default button , gets propogated here ... */ /*------------------------------------------------------------------*/ setonkeyreleased(null); event.consume(); logger.debug("enter key released on [{}]", currtaxatreeitem.gettext()); switch (currtaxatreeitem.gettype()) { case taxon: edittaxon(); break; // other cases... } break; case delete: event.consume(); logger.debug("delete key released on [{}]", currtaxatreeitem.gettext()); switch (currtaxatreeitem.gettype()) { case taxon: deletetaxon(); break; // other cases... } break; } } } } /** * here dialog - pretty straight forward... */ public class taxondefdialog extends stage { public taxondefdialog(window owner, taxatreeitem parent, taxatreeitem taxondata) { ... init(); } private void init() { initmodality(modality.application_modal); initstyle(stagestyle.utility); ... button savebtn = new button(); savebtn.settext("save"); savebtn.setdefaultbutton(true); savebtn.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { // validate ... if (valid) { taxondata.settext(term); edited = true; close(); } // -------------------------------------------------- // note - consume nothing enter key // -------------------------------------------------- e.consume(); } }); button cancelbtn = new button(); cancelbtn.settext("cancel"); cancelbtn.setcancelbutton(true); cancelbtn.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { edited = false; close(); } }); } public boolean edit() { showandwait(); return edited; } }
i cannot figure out why enter key returning after hitting enter on default button in dialog.
as work around setting key handler null , reinstating later. works klunky , not way should be... ideas?
ok, found underlying cause...
it seems button.setonaction() handler responds keypressed event , consumes event - leaving keyreleased event propagate.
changed setonkeyreleased(...) setonkeypressed(...) , problem solved!
it's little things in life count ;)
Comments
Post a Comment