java - JavaFX filter ListView File elements using FileFilter -
i hava javafx application shows folders of specific directory , watches new/deleted folders , updates listview
.
now i'm trying let user filter/search folders using textfield
.
i've done before, relevant code:
@override public void initialize(url location, resourcebundle resources) { // configure other stuff configurelistview(); } private void configurelistview() { searchfield.textproperty().addlistener((observable, oldval, newval) -> { handlesearchontype(observable, oldval, newval); }); // more stuff here } private void handlesearchontype(observablevalue observable, string oldval, string newval) { file foldertosearch = new file(config.getdlrootpath()); observablelist<file> filteredlist = fxcollections.observablearraylist(foldertosearch.listfiles( pathname -> pathname.isdirectory() && pathname.getname().contains(newval))); // seems wrong here?! if (!searchfield.gettext().isempty()) { listview.setitems(filteredlist); } else { // nothing filter listview.setitems(fxcollections.observablearraylist( foldertosearch.listfiles(pathname -> pathname.isdirectory()))); } }
this gives me strange results, e.g.:
what missing here?
thank in advance!
edit:
my custom cell factory
listview.setcellfactory(new callback<listview<file>, listcell<file>>() { @override public listcell<file> call(listview<file> list) { return new listcell<file>() { @override protected void updateitem(file t, boolean bln) { super.updateitem(t, bln); if (t != null) { setgraphic(new imageview(new image("img/folder.png"))); settext(t.getname()); } } }; } });
not sure if thing wrong, custom cell factory needs handle case cell empty:
final image image = new image("img/folder.png"); listview.setcellfactory(new callback<listview<file>, listcell<file>>() { @override public listcell<file> call(listview<file> list) { return new listcell<file>() { private final imageview imageview = new imageview(image); @override protected void updateitem(file t, boolean bln) { super.updateitem(t, bln); if (t == null) { setgraphic(null); settext(null); } else { setgraphic(imageview); settext(t.getname()); } } }; } });
the point here when start filtering, cells not empty become empty. updateitem(null, true)
invoked on cells, need clear content (otherwise keep content had before).
(for bonus, refactored don't keep loading image image file in every cell, every time user scrolls list view.)
Comments
Post a Comment