java - JavaFX Listview bind button to the index of the cell -
im trying build custom view javafx allows 2 buttons "edit user" , "view user" placed in cell.
i have adapter built having trouble assigning index button when cell created, problem when click button within cell null pointer excpetion.
edit 07/04/2014 : have updated program logic little still struggling find solution problem - index assigned correct, incorrect ( set log give me current index , fluctuates drastically, leads me conclude cell constructor unreliable)
i use following code : populatelistview():
public void populatelistview() { // 0 index each time list view repopulated // bind correct button index = 0; // refresh lists contents null lstnotes.setitems(null); // observable list containing items add observablelist notes = populateobservable(thistenant.getnotes()); // set items returned populateobservable(t); lstnotes.setitems(notes); lstnotes.setfixedcellsize(50); // use cell factory custom styling lstnotes.setcellfactory(new callback<listview, listcell>() { @override public listcell call(listview listview) { notecell ncell = new notecell(index, controller); index++; ncell.getstyleclass().add("border-bottom"); return ncell; } }); }
the comments self explanatory here - observable list of items populated via method - populateobservable(notelist n):
public observablelist populateobservable(arraylist<note> notesarray) { observablelist notes = fxcollections.observablearraylist(); // loop through users notes array , create listview item for(int = 0; < notesarray.size(); i++) { note thisnote = notesarray.get(i); notes.add(thisnote.getmessage()); } return notes; }
finally custom view constructed in cell factory (the styling irrelevant question), should noted reference controller passed can access methods - within notecell class have method delete row @ given index (this produces skewed results)
cross.setonmouseclicked(new eventhandler<mouseevent>() { @override public void handle(mouseevent mouseevent) { screensframework.loggeneral.writetofile("i removing index " + string.valueof(thisindex)); if(thistenant.removenoteat(thisindex, true)){ controller.populatelistview(); } else { controller.populatelistview(); } } });
the call populatelistview() there refresh contents of listview , rebuild dependent on tenant's updated note array.
the removenoteat() method sends delete query db , removes note tenants note array @ given index (but not correct index stated above, can't find effective way bind button listviews cell , feeble attempt @ passing index not seem work).
i hope update provides clearer scope question, please request if need more information clearer understanding of this.
edit: notecell code:
public class notecell extends listcell<string> { hbox hbox = new hbox(); hbox c = new hbox(); pane b = new hbox(); pane pane = new pane(); label msg = new label(); label date = new label(); button cross = new button(); tenant thistenant; int thisindex; string lastitem; public notecell(int index, userinfocontroller controller) { super(); try{ thistenant = controller.gettenant(); note currnote = thistenant.getnoteat(index); date = new label(currnote.getdate()); system.out.println(thistenant.getnotes()); thisindex = index; date.setstyle("-fx-text-fill: #fff !important; -fx-font-family: helvetica; -fx-font-weight: bold; -fx-font-size: 12px; -fx-background-color: #f9246b; -fx-border-color: #fe246c; -fx-padding: 8 8 6 8; -fx-border-radius: 6; -fx-background-radius: 6"); b.setstyle("-fx-padding: 10px 5px 10px 5px"); msg.setstyle("-fx-text-fill: #fafafa; -fx-font-size: 14px; -fx-font-family: 'open sans light'; -fx-label-padding: 10px"); cross.getstyleclass().add("button_close"); cross.settranslatey(20); msg.setalignment(pos.center_left); pane.settranslatey(20); b.getchildren().add(date); c.getchildren().add(cross); hbox.getchildren().addall(b, msg, pane, cross); hbox.sethgrow(pane, priority.always); // deletes note cross.setonmouseclicked(new eventhandler<mouseevent>() { @override public void handle(mouseevent mouseevent) { screensframework.loggeneral.writetofile("i removing index " + string.valueof(thisindex)); if(thistenant.removenoteat(thisindex, true)){ controller.populatelistview(); } else { controller.populatelistview(); } } }); } catch (exception e) { screensframework.logerror.writetofile(e.getmessage()); } } @override protected void updateitem(string item, boolean empty) { super.updateitem(item, empty); settext(null); // no text in label of super class if (empty) { lastitem = null; setgraphic(null); } else { lastitem = item; msg.settext(item!=null ? item : "<null>"); setgraphic(hbox); } }
}
edit: there way when creating listview cells pass current index of cell "cellfactory"?
regards, alex
every cell knows index:
public class demo extends application { listview<string> list = new listview<string>(); observablelist<string> data = fxcollections.observablearraylist( "chocolate", "salmon", "gold", "coral", "darkorchid", "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue", "blueviolet", "brown"); @override public void start(stage stage) { vbox box = new vbox(); scene scene = new scene(box, 200, 200); stage.setscene(scene); box.getchildren().addall(list); vbox.setvgrow(list, priority.always); list.setitems(data); list.setcellfactory(new callback<listview<string>, listcell<string>>() { @override public listcell<string> call(listview<string> list) { return new buttoncell(); } }); stage.show(); } static class buttoncell extends listcell<string> { @override public void updateitem(string item, boolean empty) { super.updateitem(item, empty); if (item != null) { final button btn = new button(item + " index " + getindex()); btn.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { system.out.println("i " + getitem() + " index " + getindex()); } }); setgraphic(btn); } else { setgraphic(null); } } } public static void main(string[] args) { launch(args); } }
Comments
Post a Comment