java - JavaFx8 Tableview updating ObservableList -
i have table view inside table have check box need when select row make check box selected , other check boxes deselect solve having listener on table view , iterate on list items , set checked false , selected row true problem have when called observablelist.setall(...) got these exceptions after code
accountingfilestable.getselectionmodel().selecteditemproperty().addlistener( (observable, oldvalue, newvalue) -> showaccountingfilesdetails(newvalue)); private void showaccountingfilesdetails(accountingfiles newvalue) { accountingfiles curraccfile = accountingfilestable.getselectionmodel().getselecteditem(); accountingfilestable.getselectionmodel().clearselection(); system.out.println(curraccfile.getaccountingfilesname()); (accountingfiles af : accountingdata) { mainem.gettransaction().begin(); af.setaccountingfilesactivestatus(false); mainem.gettransaction().commit(); //accountingdata.set(accountingdata.indexof(af), af); system.out.println("in looooooooooooop"); } mainem.gettransaction().begin(); curraccfile.setaccountingfilesactivestatus(true); mainem.gettransaction().commit(); list<accountingfiles> listofaccountingfiles = mainem.createnamedquery("accountingfiles.findall").getresultlist(); accountingdata.setall(listofaccountingfiles); } exception in thread "javafx application thread" java.lang.indexoutofboundsexception @ com.sun.javafx.scene.control.readonlyunbackedobservablelist.sublist(readonlyunbackedobservablelist.java:136) @ javafx.collections.listchangelistener$change.getaddedsublist(listchangelistener.java:242) @ javafx.scene.control.tableview$tableviewarraylistselectionmodel.handleselectedcellslistchangeevent(tableview.java:2959) @ javafx.scene.control.tableview$tableviewarraylistselectionmodel.clearandselect(tableview.java:2395) @ javafx.scene.control.tableview$tableviewselectionmodel.clearandselect(tableview.java:1898) @ com.sun.javafx.scene.control.behavior.tablecellbehaviorbase.simpleselect(tablecellbehaviorbase.java:215) @ com.sun.javafx.scene.control.behavior.tablecellbehaviorbase.doselect(tablecellbehaviorbase.java:148) @ com.sun.javafx.scene.control.behavior.cellbehaviorbase.mousepressed(cellbehaviorbase.java:150) @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(behaviorskinbase.java:95) @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(behaviorskinbase.java:89) @ com.sun.javafx.event.compositeeventhandler$normaleventhandlerrecord.handlebubblingevent(compositeeventhandler.java:218) @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:80) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:238) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74) @ com.sun.javafx.event.eventutil.fireevent(eventutil.java:54) @ javafx.event.event.fireevent(event.java:198) @ javafx.scene.scene$mousehandler.process(scene.java:3758) @ javafx.scene.scene$mousehandler.access$1500(scene.java:3486) @ javafx.scene.scene.impl_processmouseevent(scene.java:1762) @ javafx.scene.scene$scenepeerlistener.mouseevent(scene.java:2495) @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:350) @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:275) @ java.security.accesscontroller.doprivileged(native method) @ com.sun.javafx.tk.quantum.glassvieweventhandler.lambda$handlemouseevent$350(glassvieweventhandler.java:385) @ com.sun.javafx.tk.quantum.glassvieweventhandler$$lambda$176/1128584231.get(unknown source) @ com.sun.javafx.tk.quantum.quantumtoolkit.runwithoutrenderlock(quantumtoolkit.java:404) @ com.sun.javafx.tk.quantum.glassvieweventhandler.handlemouseevent(glassvieweventhandler.java:384) @ com.sun.glass.ui.view.handlemouseevent(view.java:555) @ com.sun.glass.ui.view.notifymouse(view.java:927) @ com.sun.glass.ui.win.winapplication._runloop(native method) @ com.sun.glass.ui.win.winapplication.lambda$null$145(winapplication.java:101) @ com.sun.glass.ui.win.winapplication$$lambda$36/1030870354.run(unknown source) @ java.lang.thread.run(thread.java:745)
please me out of have been stocked days in these exceptions way table view works fine these exceptions still exists.
here full class implementation package front.view;
import back.entities.accountingfiles; import back.entities.accountingfilesdetails; import back.utility.connector; import hal.main.mainapp; import java.net.url; import java.time.localdate; import java.time.format.datetimeformatter; import java.util.list; import java.util.resourcebundle; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.control.contentdisplay; import javafx.scene.control.label; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.cell.checkboxtablecell; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.control.cell.textfieldtablecell; import javax.persistence.entitymanager; public class accountingfilesformcontroller implements initializable { @fxml private tableview<accountingfiles> accountingfilestable; @fxml private tableview<accountingfilesdetails> accountingfilesdetailstable; @fxml private tablecolumn<accountingfilesdetails, boolean> accountingfilesdetailsselectedfile; @fxml private tablecolumn<accountingfilesdetails, localdate> accountingfilesdetailsstartdate; @fxml private tablecolumn<accountingfilesdetails, localdate> accountingfilesdetailsenddate; @fxml private tablecolumn<accountingfilesdetails, string> accountingfilesdetailsfilename; @fxml private tablecolumn<accountingfilesdetails, string> accountingfilesdetailslocation; @fxml private tablecolumn<accountingfilesdetails, string> accountingfilesdetailsbackuplocation; @fxml private tablecolumn<accountingfilesdetails, boolean> accountingfilesdetailsclosed; @fxml private tablecolumn<accountingfiles, boolean> currentaccountingselectedfile; @fxml private tablecolumn<accountingfiles, string> accountingfilename; @fxml private tablecolumn<accountingfiles, string> accountingfilepassword; private observablelist<accountingfiles> accountingdata = fxcollections.observablearraylist(); private observablelist<accountingfilesdetails> accountingdetails = fxcollections.observablearraylist(); entitymanager mainem = connector.getmainentitymanager(); ; mainapp mainapp; public void setmainapp(mainapp mainapp) { this.mainapp = mainapp; system.out.println(accountingfilestable); } @override public void initialize(url url, resourcebundle rb) { accountingfilename.setcellvaluefactory(new propertyvaluefactory<accountingfiles, string>("accfilename")); accountingfilename.setcellfactory(textfieldtablecell.<accountingfiles>fortablecolumn()); accountingfilepassword.setcellvaluefactory(new propertyvaluefactory<accountingfiles, string>("accfilepassword")); accountingfilepassword.setcellfactory(param -> new passwordlabelcell()); currentaccountingselectedfile.setcellvaluefactory(new propertyvaluefactory<accountingfiles, boolean>("currentaccselectedfile")); currentaccountingselectedfile.setcellfactory(checkboxtablecell.fortablecolumn(currentaccountingselectedfile)); currentaccountingselectedfile.seteditable(false); accountingfilestable.seteditable(true); addingitems(); accountingfilestable.getselectionmodel().selecteditemproperty().addlistener( (observable, oldvalue, newvalue) -> showaccountingfilesdetails(newvalue)); } public void addingitems() { list<accountingfiles> listofaccountingfiles = mainem.createnamedquery("accountingfiles.findall").getresultlist(); accountingdata.addall(listofaccountingfiles); accountingfilestable.setitems(accountingdata); } @fxml private void handleselect() { system.out.println("begin handleselect"); accountingfiles curraccfile = accountingfilestable.getselectionmodel().getselecteditem(); accountingfilestable.getselectionmodel().clearselection(); system.out.println("after clearselection"); system.out.println(curraccfile.getaccountingfilesname()); (accountingfiles af : accountingdata) { mainem.gettransaction().begin(); af.setaccountingfilesactivestatus(false); mainem.gettransaction().commit(); system.out.println("in looooooooooooop"); } system.out.println("after loop finished"); mainem.gettransaction().begin(); curraccfile.setaccountingfilesactivestatus(true); mainem.gettransaction().commit(); system.out.println("after lastcommit of finished"); list<accountingfiles> listofaccountingfiles = mainem.createnamedquery("accountingfiles.findall").getresultlist(); accountingdata.setall(listofaccountingfiles); system.out.println("after setall method"); } private void showaccountingfilesdetails(accountingfiles newvalue) { handleselect(); } } } public class passwordlabelcell extends tablecell<accountingfiles, string> { private label label; public passwordlabelcell() { label = new label(); this.setcontentdisplay(contentdisplay.graphic_only); this.setgraphic(null); } private string gendotstring(int len) { string dots = ""; (int = 0; < len; i++) { dots += "\u2022"; } return dots; } @override protected void updateitem(string item, boolean empty) { super.updateitem(item, empty); if (!empty) { label.settext(gendotstring(item.length())); setgraphic(label); } else { setgraphic(null); } } } }
that full class . have tried add button , on actionlistener called showaccountingfilesdetails worked , no exception occurred didnt understand why , dont want select row , press button code embarrassing ...
hey guys reply have made mcve , there
import java.util.arrays; import java.util.list; import javafx.application.application; import javafx.beans.observable; import javafx.beans.property.booleanproperty; import javafx.beans.property.simplebooleanproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.listchangelistener; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.cell.checkboxtablecell; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.layout.borderpane; import javafx.stage.stage; import javafx.util.callback; public class tablewithcheckbox extends application { final tableview<player> table = new tableview<player>(); observablelist<player> data; @override public void start(stage primarystage) { final borderpane root = new borderpane(); table.setitems(createdata()); final tablecolumn<player, string> firstnamecolumn = new tablecolumn<>("first name"); final tablecolumn<player, string> lastnamecolumn = new tablecolumn<>("last name"); final tablecolumn<player, boolean> selectedcolumn = new tablecolumn<>("selected"); firstnamecolumn.setcellvaluefactory(new propertyvaluefactory<player, string>("firstname")); lastnamecolumn.setcellvaluefactory(new propertyvaluefactory<player, string>("lastname")); selectedcolumn.setcellvaluefactory(new propertyvaluefactory<player, boolean>("selected")); final callback<tablecolumn<player, boolean>, tablecell<player, boolean>> cellfactory = checkboxtablecell.fortablecolumn(selectedcolumn); selectedcolumn.setcellfactory(new callback<tablecolumn<player, boolean>, tablecell<player, boolean>>() { @override public tablecell<player, boolean> call(tablecolumn<player, boolean> column) { tablecell<player, boolean> cell = cellfactory.call(column); cell.setalignment(pos.center); return cell; } }); selectedcolumn.setcellfactory(cellfactory); selectedcolumn.seteditable(false); table.seteditable(true); table.getcolumns().addall(selectedcolumn, firstnamecolumn, lastnamecolumn); root.setcenter(table); button button1 = new button("add"); button1.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { data.add(new player("hbghj", "hjhbbccc77", false)); } }); root.settop(button1); primarystage.setscene(new scene(root, 400, 600)); primarystage.show(); } public static void main(string[] args) { launch(args); } private observablelist<player> createdata() { list<player> players = arrays.aslist( new player("hugo", "lloris", true), new player("brad", "friedel", false), new player("kyle", "naughton", false), new player("younes", "kaboul", false), new player("benoit", "assou-ekotto", false), new player("jan", "vertonghen", false), new player("michael", "dawson", false), new player("william", "gallas", false), new player("kyle", "walker", false), new player("scott", "parker", false), new player("mousa", "dembele", false), new player("sandro", "cordeiro", false), new player("tom", "huddlestone", false), new player("gylfi", "sigurdsson", false), new player("gareth", "bale", false), new player("aaron", "lennon", false), new player("jermane", "defoe", false), new player("emmanuel", "adebayor", false) ); data = fxcollections.<player>observablearraylist( new callback<player, observable[]>() { @override public observable[] call(player player) { return new observable[]{player.selectedproperty()}; } } ); data.addall(players); data.addlistener(new listchangelistener<player>() { @override public void onchanged( javafx.collections.listchangelistener.change<? extends player> change) { system.out.println("list changed"); while (change.next()) { if (change.wasupdated()) { system.out.println("what code here"); system.out.println(); } } } }); table.getselectionmodel().selecteditemproperty().addlistener(new changelistener() { @override public void changed(observablevalue observablevalue, object oldvalue, object newvalue) { player currplayer = table.getselectionmodel().getselecteditem(); table.getselectionmodel().clearselection(); system.out.println("after clearselection"); (player pl : players) { pl.setselected(false); } currplayer.setselected(true); } }); return data; } public static class player { private final stringproperty firstname; private final stringproperty lastname; private final booleanproperty selected; player(string firstname, string lastname, boolean international) { this.firstname = new simplestringproperty(this, "firstname", firstname); this.lastname = new simplestringproperty(this, "lastname", lastname); this.selected = new simplebooleanproperty(this, "selected", international); } public string getfirstname() { return firstname.get(); } public void setfirstname(string firstname) { this.firstname.set(firstname); } public stringproperty firstnameproperty() { return firstname; } public string getlastname() { return lastname.get(); } public void setlastname(string lastname) { this.lastname.set(lastname); } public stringproperty lastnameproperty() { return lastname; } public boolean isselected() { return selected.get(); } public void setselected(boolean international) { this.selected.set(international); } public booleanproperty selectedproperty() { return selected; } @override public string tostring() { return firstname.get() + " " + lastname.get() + (selected.get() ? " (injured)" : ""); } } }
the same proplem occur , same exceptions please
don't have time clean code. please bear me. here working model of whatever posted.
import java.util.arrays; import java.util.list; import javafx.application.application; import javafx.beans.observable; import javafx.beans.property.booleanproperty; import javafx.beans.property.simplebooleanproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.listchangelistener; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.selectionmode; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.cell.checkboxtablecell; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.layout.borderpane; import javafx.stage.stage; import javafx.util.callback; public class tablewithcheckbox extends application { final tableview<player> table = new tableview<player>(); observablelist<player> data; @override public void start(stage primarystage) { final borderpane root = new borderpane(); table.setitems(createdata()); final tablecolumn<player, string> firstnamecolumn = new tablecolumn<>( "first name"); final tablecolumn<player, string> lastnamecolumn = new tablecolumn<>( "last name"); final tablecolumn<player, boolean> selectedcolumn = new tablecolumn<>( "selected"); firstnamecolumn .setcellvaluefactory(new propertyvaluefactory<player, string>( "firstname")); lastnamecolumn .setcellvaluefactory(new propertyvaluefactory<player, string>( "lastname")); selectedcolumn .setcellvaluefactory(new propertyvaluefactory<player, boolean>( "selected")); final callback<tablecolumn<player, boolean>, tablecell<player, boolean>> cellfactory = checkboxtablecell .fortablecolumn(selectedcolumn); selectedcolumn .setcellfactory(new callback<tablecolumn<player, boolean>, tablecell<player, boolean>>() { @override public tablecell<player, boolean> call( tablecolumn<player, boolean> column) { tablecell<player, boolean> cell = cellfactory .call(column); cell.setalignment(pos.center); return cell; } }); selectedcolumn.setcellfactory(cellfactory); selectedcolumn.seteditable(false); table.seteditable(true); table.getcolumns().addall(selectedcolumn, firstnamecolumn, lastnamecolumn); table.getselectionmodel().setselectionmode(selectionmode.multiple); root.setcenter(table); button button1 = new button("add"); button1.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent event) { data.add(new player("hbghj", "hjhbbccc77", false)); } }); root.settop(button1); primarystage.setscene(new scene(root, 400, 600)); primarystage.show(); } public static void main(string[] args) { launch(args); } private observablelist<player> createdata() { list<player> players = arrays.aslist( new player("hugo", "lloris", true), new player("brad", "friedel", false), new player("kyle", "naughton", false), new player("younes", "kaboul", false), new player("benoit", "assou-ekotto", false), new player("jan", "vertonghen", false), new player("michael", "dawson", false), new player("william", "gallas", false), new player("kyle", "walker", false), new player("scott", "parker", false), new player("mousa", "dembele", false), new player("sandro", "cordeiro", false), new player("tom", "huddlestone", false), new player("gylfi", "sigurdsson", false), new player("gareth", "bale", false), new player("aaron", "lennon", false), new player("jermane", "defoe", false), new player("emmanuel", "adebayor", false)); data = fxcollections .<player> observablearraylist(new callback<player, observable[]>() { @override public observable[] call(player player) { return new observable[] { player.selectedproperty() }; } }); data.addall(players); data.addlistener(new listchangelistener<player>() { @override public void onchanged( javafx.collections.listchangelistener.change<? extends player> change) { system.out.println("list changed"); while (change.next()) { if (change.wasupdated()) { system.out.println("what code here"); system.out.println(); } } } }); table.getselectionmodel().selecteditemproperty() .addlistener(new changelistener<player>() { @override public void changed(observablevalue observablevalue, player oldvalue, player newvalue) { player currplayer = table.getselectionmodel() .getselecteditem(); system.out.println("after clearselection"); currplayer.setselected(true); } }); return data; } public static class player { private final stringproperty firstname; private final stringproperty lastname; private final booleanproperty selected; player(string firstname, string lastname, boolean international) { this.firstname = new simplestringproperty(this, "firstname", firstname); this.lastname = new simplestringproperty(this, "lastname", lastname); this.selected = new simplebooleanproperty(this, "selected", international); } public string getfirstname() { return firstname.get(); } public void setfirstname(string firstname) { this.firstname.set(firstname); } public stringproperty firstnameproperty() { return firstname; } public string getlastname() { return lastname.get(); } public void setlastname(string lastname) { this.lastname.set(lastname); } public stringproperty lastnameproperty() { return lastname; } public boolean isselected() { return selected.get(); } public void setselected(boolean international) { this.selected.set(international); } public booleanproperty selectedproperty() { return selected; } @override public string tostring() { return firstname.get() + " " + lastname.get() + (selected.get() ? " (injured)" : ""); } } }
this doesn't un-check
other checkboxes while selecting checkbox because didn't think idea. if want work way, add loop back.
for (player pl : players) { pl.setselected(false); }
Comments
Post a Comment