combobox - JavaFX: Capture "Enter" key pressed -
i need maintain indeterminably sized list of strings. figured best way through combo box take user input, , in turn add together user input upon "enter" keystroke detected combobox list of items, , allow user remove items "delete" keystroke.
i had hoped simple task handled so:
this.cbx.setonkeytyped((keyevent e) -> { switch(e.getcode()){ case enter: this.cbx.getitems().add(this.cbx.valueproperty().get()); this.cbx.valueproperty().set(""); e.consume(); break; case delete: if (this.cbx.getitems().contains( this.cbx.valueproperty().get() )) this.cbx.getitems().remove(this.cbx.valueproperty().get()); this.cbx.valueproperty().set(""); e.consume(); break; } });
unfortunately, come in not trigger event. mistaken. tried onkeypressed
, , did not work either. need capture when "enter" , "delete" pressed (it picks "shift" fine maddening).
have tried
if(e.getcode().equals(keycode.enter)){ ... } else if (e.getcode().equals(keycode.delete)){ ... }
no love.
edit 2:based on james_d's reply below set me on right course, in order accomplish seeking do, employed next method:
combobox<string> cb = new combobox<>(); cb.seteditable(true); cb.geteditor().addeventfilter(keyevent.key_pressed, (keyevent e) -> { switch(e.getcode()){ case enter:{ if (cb.getitems().contains(cb.geteditor().gettext())) e.consume(); else{ cb.getitems().add(cb.geteditor().gettext()); cb.geteditor().clear(); e.consume(); } break; } case delete:{ if (e.iscontroldown() && cb.getitems().contains(cb.geteditor().gettext())) cb.getitems().remove(cb.geteditor().gettext()); else if (e.isaltdown()) cb.getitems().clear(); if (e.iscontroldown() || e.isaltdown()){ cb.geteditor().clear(); e.consume(); } break; } } });
are looking have editable combo box, adds items popup list when user types in items not there?
if so, try:
import javafx.application.application; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.combobox; import javafx.scene.layout.hbox; import javafx.stage.stage; public class updatingcombobox extends application { @override public void start(stage primarystage) { combobox<string> combo = new combobox<>(); combo.seteditable(true); combo.valueproperty().addlistener((obs, oldvalue, newvalue) -> { if (newvalue != null && ! combo.getitems().contains(newvalue)) { combo.getitems().add(newvalue); } }); hbox root = new hbox(combo); root.setalignment(pos.top_center); primarystage.setscene(new scene(root, 350, 150)); primarystage.show(); } public static void main(string[] args) { launch(args); } }
for deleting, first thing inquire if want functionality you've described it. users typically expect pressing delete in editable combo box delete next character, not remove item exclusively list. if want this, have hands little more dirty , utilize key listener. reason, adding key listener combo box straight seems have unpredictable results; works if add together text field underlying editable combo box:
combo.geteditor().addeventfilter(keyevent.key_pressed, event -> { if (event.getcode() == keycode.delete) { combo.getitems().remove(combo.getvalue()); event.consume(); } });
combobox javafx keyevent enter
No comments:
Post a Comment