java - How to manipulate Object through the selectedItem of a Combobox -
just in case want me, appreciate lot that, because i'm noob in java , trying learn concept and, in same time, maintain code clean , read. if think it's stupid question because have wrong approach coding, care opinion , if explain me why try experience.
now questions is:
i have mainapp containing observablelist of persons , observablelist of events
public class mainapp extends application { private stage primarystage; private borderpane rootlayout; private observablelist<evento> eventi = fxcollections.observablearraylist(); private observablelist<person> persons = fxcollections.observablearraylist(); public observablelist<evento> geteventi() { return eventi; } public observablelist<person> getpersons() { return persons; } public static void main(string[] args) {launch(args);} public void start(stage primarystage){ this.primarystage = primarystage; this.primarystage.settitle("bettator events"); person test = new person("daniele", "giaquinto"); test.geteventi().add(new evento("danevento", 1.0, "testconto")); persons.add(test); person test2 = new person("roberto", "sellitto"); test2.geteventi().add(new evento("robevento", 31.0, "testco")); persons.add(test2); initrootlayout(); showeventilayout(); }
person object have list of events too, created after, because trying create responsive gui, in case, instead of 1 list of events, every single person have events list.
public class person { private stringproperty firstname; private stringproperty lastname; private stringproperty nickname; private observablelist<evento> eventi = fxcollections.observablearraylist();
i created eventylayout combobox , tableview, when change combobox, using listener, tableview populated events list of person object, did filling mainapp events list selected person events list
(eventicontroller)
public class eventilayoutcontroller { private mainapp mainapp; @fxml private combobox<person> utentecmb; @fxml private tableview<evento> eventitbl; @fxml private tablecolumn<evento, string> eventocol; @fxml private tablecolumn<evento, double> importocol; @fxml private tablecolumn<evento, string> contocol; @fxml void initialize() { eventocol.setcellvaluefactory(celldata -> celldata.getvalue().nomeproperty()); importocol.setcellvaluefactory(celldata -> celldata.getvalue().importoproperty().asobject()); contocol.setcellvaluefactory(celldata -> celldata.getvalue().contoproperty()); utentecmb.setcellfactory((combobox) -> new listcell<person>() { //cut stackoverflow }); utentecmb.setconverter(new stringconverter<person>() { //cut stackoverflow }); utentecmb.getselectionmodel().selecteditemproperty().addlistener((observable, oldvalue, newvalue) -> { mainapp.geteventi().clear(); mainapp.geteventi().addall(newvalue.geteventi()); }); } public void setmainapp(mainapp mainapp){ this.mainapp = mainapp; eventitbl.setitems(mainapp.geteventi()); utentecmb.setitems(mainapp.getpersons()); }
}
before project without possibility switch user, , created "handleaddevent" add event mainapp list, in rootlayoutcontroller (the 1 show me menubar , menu item, add button is)
public class rootlayoutcontroller { private mainapp mainapp; @fxml // resourcebundle given fxmlloader private resourcebundle resources; @fxml // url location of fxml file given fxmlloader private url location; @fxml // method called fxmlloader when initialization complete void initialize() { } /** * needed pass mainapp controller ad use public variable */ public void setmainapp(mainapp mainapp){ this.mainapp = mainapp; } /** * open dialog add new event, add event collection */ @fxml private void handleadd() { evento evento = new evento(); boolean okclicked = mainapp.showeventiediteditdialog(evento); if (okclicked) mainapp.geteventi().add(evento); } }
but tried go more deep learning, , need add event directly person->event, if try add using same function, add mainapp events list , not person events list, if mainapp persons list filled person events.
i wondering if there way pass pointer observablelist mainapp or there simple-way reach objective.
rootlayoutcontroller didn't know there combobox when try add event, , mainapp too, didn't have know there combobox, how selectedperson , add event events list?
should create "activeperson" mainapp , switch everytime user change item combobox? seem me , "hard coding" method.
how do?
well, there no 1 right way of designing app. of course there design principles, should try follow. there lot of information topic, won't problem find it.
specific case, move combobox , person
selection logic eventilayoutcontroller
rootlayoutcontroller
. way have selected person
instance in root controller , can notify event controller when change occurred , provide selected person
it. notify can done either having eventilayoutcontroller
instance directly in rootlayoutcontroller
or using event bus approach example.
instead of mainapp.geteventi().addall(newvalue.geteventi());
i'd use property binding (or bidirectional binding, depending on logic). don't quite understand why need separate eventi
collection in mainapp, if filling selected person events anyway.
Comments
Post a Comment