javafx binding combobox itemsProperty -


i have customized combobox in order add , remove item in combobox. bind customized combobox, not working , don't understand happen.

here sample example :

public class cbbxeditsuppr extends combobox<boxitem> {  private textfield editor = null; private observablelist<boxitem> items = null;    /**  * constructeur.  *   * @param addeditems observablelist<string>  * @param prefwidth double largeur préférée  */ public cbbxeditsuppr(observablelist<string> addeditems, final double prefwidth) {     // initialisation des attributs     editor = this.geteditor();     items = this.getitems();     this.setprefwidth(prefwidth);     // initialisation du contenu de la cellule     this.setcellfactory(new callback<listview<boxitem>, listcell<boxitem>>() {         @override         public listcell<boxitem> call(listview<boxitem> p) {             final listcell<boxitem> cell = new listcell<boxitem>() {                 @override                 protected void updateitem(boxitem t, boolean bln) {                     super.updateitem(t, bln);                     if (t != null) {                         setgraphic(t.getbtn());                         settext(t.getitem());                     } else {                         setgraphic(null);                     }                 }             };             return cell;         }     });     // déininition du converter     this.setconverter(new stringconverter<boxitem>() {         @override         public string tostring(boxitem cbbx) {             if (cbbx == null) {                 return null;             } else {                 return cbbx.getitem();             }         }         @override         public boxitem fromstring(string id) {             if (id != null) {                 final boxitem box = new boxitem(id, items);                 return box;             } else {                 return null;             }          }     });     // permet de prendre en compte la touche enter, et ajouter des valeurs la liste     this.addeventfilter(keyevent.key_pressed, new eventhandler<keyevent>() {         public void handle(final keyevent event) {             if (event != null && event.getcode().equals(keycode.enter)) {                 if (editor.gettext().trim().length() > 0) {                     additem(editor.gettext());                     editor.clear();                 }             } else if  (event != null && event.getcode().equals(keycode.down)) {                 showpopupmenu();             }         }     });     // propriétés editable et selection du premier element     this.seteditable(true);     /* ajout des valeurs la liste d'items */     if (addeditems != null && addeditems.size() > 0) {         (string stg : addeditems) {             if (stg != null) {                 final boxitem hbox = new boxitem(stg, items);                 items.add(hbox);             }         }     }     this.getselectionmodel().selectfirst(); }  private void showpopupmenu(){     if (!this.isshowing()) {         this.show();     } }  /**  * ajoute un item à la liste  *   * @param stg string nom de l'item  */ public void additem(string stg) {     if (stg != null) {         items.add(new boxitem(stg, items));     } }   /**  * retourne la description du contenu de la liste  */ public string tostring() {     final stringbuilder stgbuilder = new stringbuilder("[ ");     if (items != null && items.size() > 0) {         final boxitem lastitem = items.get(items.size() - 1);         (boxitem item : items) {             if (item != null) {                 stgbuilder.append(item.getitem());                 if (!item.equals(lastitem)) {                     stgbuilder.append(", ");                 }             }         }     }     stgbuilder.append(" ]");     return stgbuilder.tostring();  } } 

and

public class comboboxsample extends application {      public static void main(string[] args) {      launch(args); }   @override public void start(stage stage) {     stage.settitle("comboboxsample");     scene scene = new scene(new group(), 450, 250);      cbbxeditsuppr cbboxleft = new cbbxeditsuppr(fxcollections.observablearraylist(new arraylist()), 200);     cbbxeditsuppr cbboxright = new cbbxeditsuppr(fxcollections.observablearraylist(new arraylist()), 200);      cbboxleft.itemsproperty().bindbidirectional(cbboxright.itemsproperty());     gridpane grid = new gridpane();     grid.setvgap(4);     grid.sethgap(10);     grid.setpadding(new insets(5, 5, 5, 5));     grid.add(new label("to: "), 0, 0);     grid.add(cbboxleft, 1, 0);     grid.add(cbboxright, 2, 0);      group root = (group) scene.getroot();     root.getchildren().add(grid);     stage.setscene(scene);     stage.show();  } } 

when add value in right combobox working not in left combobox. have advice please ?

the issue "shadow" items field. there 2 properties called items in combo box subclass: 1 define , 1 inherit. when call itemsproperty() getting property wrapping inherited items, , binding that. in class implementation, refer shadow items field, not bound.

you should remove items field entirely class, , refer inherited 1 calling getitems().

for example, do

if (addeditems != null && addeditems.size() > 0) {     (string stg : addeditems) {         if (stg != null) {             final boxitem hbox = new boxitem(stg, items);             getitems().add(hbox);         }     } } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -