listview - Strange behaviour of observable lists -
i bound observablelist listview perform tasks on listview. type of listview , observablelist model defined in model layer.so use cell factory render graphical node model objects. should add item observablelist user enter text in textfield. problem when user enter new text , add items observablelist additional unexpectec items apear @ listview. if add items observable list manualy out of event handler, list view works correctly. think because of threading issues cant solve it.
here simple example produce problem:
fxmldocumentcontroller.java:
public class fxmldocumentcontroller implements initializable { @fxml listview<string> list; observablelist<string> ol; @fxml textfield text; @override public void initialize(url url, resourcebundle rb) { ol = fxcollections.observablearraylist(); list.setitems(ol); list.setcellfactory((listview<string> param) -> { listcell<string> lc = new listcell<string>(){ @override protected void updateitem(string item, boolean empty){ super.updateitem(item, empty); if(!empty){ system.out.println("hi"); parent root = null; try { root = fxmlloader.load(getclass().getresource("entry.fxml")); } catch (ioexception ex) { } ((label)(root.lookup("#label"))).settext(item); setgraphic(root); } } }; return lc; }); text.setonaction((actionevent event) -> { ol.add(text.gettext()); text.clear(); }); } }
fxmldoument.fxml:
<anchorpane id="anchorpane" prefheight="511.0" prefwidth="420.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="testapp.fxmldocumentcontroller"> <children> <listview fx:id="list" layoutx="68.0" layouty="14.0" prefheight="391.0" prefwidth="284.0" /> <textfield fx:id="text" layoutx="110.0" layouty="419.0" /> </children> </anchorpane>
entry.fxml:
<anchorpane id="anchorpane" prefheight="50.0" prefwidth="250.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <label fx:id="label" layoutx="70.0" layouty="18.0" prefheight="15.0" prefwidth="178.0" anchorpane.rightanchor="49.0" /> </children> </anchorpane>
thanks in advances
Comments
Post a Comment