swing - Java - OOP and MVC -


i trying understand how can implement mvc java application, read couple of tutorials , mvc hello worlds, still not sure many things, glad if me understand this.

let's have simple gui application, used storing , working various buildings. can add building chosing type list , pressing button. these buldings stored in arraylist, displayed in gui , editable (number of rooms, number of floors ..... not important). buildings displayed in jcombobox , after choosing building, setting panel building appear.

so far have 2 "components". buildings (container) , building. have created buildingsmodel class, holds buildings, has methods work them , notifies observers after change. have buildingsview class, observing buildingsmodel. have buldingscontroller class constructor method, takes buildingsmodel , buildingsview parameters, binds view model observer, create initial buildings , add listeners view.

now don't know how continue. there few things not happy about.

  1. i have binded listener button, current selection jlist in view, create new object (xxxbuildingmodel) , add buildingsmodel. however, jlist contains string representations of building types , avoid long if-else statemenets , finding right class string, had use reflection. (each building type has own class extends buildingmodel.) there better way it?

  2. the second listener bound jcombobox contains created building instances. after selecting building in combobox, want display settings form building. guess there should view class associated buildingmodel display current settings (model's state). not sure, how buildingscontroller's constructor context. have access building's model, how should "find" right view instance , display it? maybe doing wrong , combobox shouldn't contain models controllers (which has access both model , view), in case call controller's view method, needed data model, passes view , display it. don't if should work concrete building instance model, or controller or custom class encapsulates these 3 components (controller, model, view) ...

here important parts of code. didn't include buildingmodel class , subclasses, because blank classes tostring() method.

    public class buildingscontroller {         public buildingscontroller(final buildingsmodel model, final buildingsview view) {             class addbuildingbuttonactionlistener implements actionlistener {                 @override                 public void actionperformed(actionevent e) {                     string selectedbuildingtype;                     if ((selectedbuildingtype = view.getselectedbuildingtype()) != null) {                         try {                             buildingmodel newbuilding = (buildingmodel) class.forname("building.models." + selectedbuildingtype + "buildingmodel").newinstance();                             model.addbuilding(newbuilding);                          } catch (instantiationexception ex) {                             //addtoerrorlog                         } catch (illegalaccessexception ex) {                             //addtoerrorlog                         } catch (classnotfoundexception ex) {                             //addtoerrorlog                         }                     } else {                         //addtolog - no_building_selected                     }                 }             }              class buildingcomboboxselectlistener implements actionlistener {                 @override                 public void actionperformed(actionevent e) {                     buildingmodel selectedbuilding;                     if ((selectedbuilding = view.getselectedbuilding()) != null) {                         //display current building form???                     }                 }             }             model.addobserver(view);             model.addbuilding(new hospitalbuildingmodel());             model.addbuilding(new schoolbuildingmodel());              view.fillbuildingtypeslist(buildingsmodel.getallbuildingstypes());             view.addaddbuildingbuttonlistener(new addbuildingbuttonactionlistener());             view.addactivebuildingslistener(new buildingcomboboxselectlistener());         }     }     public class buildingsmodel extends observable {         private static string[] allbuildingstypes = {"school", "hospital", "stadion"};         private arraylist<buildingmodel> buildings = new arraylist<buildingmodel>();          public void addbuilding(buildingmodel building){             this.buildings.add(building);             this.setchanged();             this.notifyobservers();         }         public buildingmodel[] getallbuildings(){             return this.buildings.toarray(new buildingmodel[this.buildings.size()]);         }         public static string[] getallbuildingstypes(){             return buildingsmodel.allbuildingstypes;         }      }     public class buildingsview implements observer {         private static string name = "buldings";         private static string addbuildingbuttontext = "add building";         private jcombobox allactivebuildings = new jcombobox();         private jlist buildingtypeslist = new jlist();         private jbutton addbuildingbutton = new jbutton(buildingsview.addbuildingbuttontext);           @override         public void update(observable model, object o){             buildingmodel[] allbuildings = ((buildingsmodel) model).getallbuildings();             this.allactivebuildings.removeallitems();             for(buildingmodel building : allbuildings){                 this.allactivebuildings.additem(building);             }         }         public string getselectedbuildingtype(){             return (string) this.buildingtypeslist.getselectedvalue();         }         public buildingmodel getselectedbuilding(){             return (buildingmodel) this.allactivebuildings.getselecteditem();         }          public void fillbuildingtypeslist(string[] buildingtypes){             this.buildingtypeslist.setlistdata(buildingtypes);         }          public void addaddbuildingbuttonlistener(actionlistener l){             this.addbuildingbutton.addactionlistener(l);         }         public void addactivebuildingslistener(actionlistener l){             this.allactivebuildings.addactionlistener(l);         }         public jcomponent display(){             jpanel panel = new jpanel();             border border = borderfactory.createcompoundborder(borderfactory.createtitledborder(buildingsview.name), borderfactory.createemptyborder(5, 5, 5, 5));             panel.setborder(border);             panel.add(this.allactivebuildings);             panel.add(this.buildingtypeslist);             panel.add(this.addbuildingbutton);              panel.setpreferredsize(new dimension(200, 262));              return panel;         }     } 

main:

    public static void main(string[] args) {         buildingsview buildingsview = new buildingsview();         buildingsmodel buildingsmodel = new buildingsmodel();         buildingscontroller buldingscontroller = new buildingscontroller(buildingsmodel, buildingsview);           mainwindow window = new mainwindow("mvc test", buildingsview);         window.generatedefaultlayout();         window.showmainwindow();      } 

window:

public class mainwindow extends jframe {     private jpanel buildingspanel = new jpanel();     private buildingsview buildingsview;      public mainwindow(string title, buildingsview buildingsview) {         this.buildingsview = buildingsview;         this.settitle(title);         this.setdefaultcloseoperation(jframe.exit_on_close);     }     public void generatedefaultlayout(){         this.setlayout(new flowlayout());         this.setpreferredsize(new dimension(1200, 920));         this.addbuildingspanel();     }     public void addbuildingspanel(){         this.buildingspanel.add(this.buildingsview.display());         this.add(this.buildingspanel);     }     public void showmainwindow(){         this.pack();         this.setvisible(true);     } } 

thanks :)

i have binded listener button, current selection jlist in view, create new object (xxxbuildingmodel) , add buildingsmodel. however, jlist contains string representations of building types , avoid long if-else statemenets , finding right class string, had use reflection. (each building type has own class extends buildingmodel.) there better way it?

yes. don't have jlist hold strings rather have hold building (non-gui) objects. use listcellrenderer tell jlist how best display each building. when user selects item jlist, they're selecting actual building object , not string representation of it. don't consider reflection of this.

the second listener bound jcombobox contains created building instances. after selecting building in combobox, want display settings form building. guess there should view class associated buildingmodel display current settings (model's state). not sure, how buildingscontroller's constructor context.

you have abstractaction class in control class , use listener jcombobox (see answer previous question here more on this).

i have access building's model, how should "find" right view instance , display it?

you selected building listener, , listener notify gui change views after selection calling public methods on view.

maybe doing wrong , combobox shouldn't contain models controllers (which has access both model , view),

the combo box's listeners part of control, yes. , have access model , view.

in case call controller's view method, needed data model, passes view , display it.

i'm not sure enough specifics of program able answer this.


edit 1
on review of code, see potential problem:

    private static string[] allbuildingstypes = {"school", "hospital",              "stadion"};     private arraylist<buildingmodel> buildings = new arraylist<buildingmodel>(); 

you should not working strings here rather well-behaved object-oriented building class, 1 has name string, , other properties important in whatever properties building objects need. model should deal collection of building objects rather string representation of buildings. views display properties of these buildings , allow user able update properties allow them change.

this goes core of program's structure, , feel important, , doing require change everything, in long run worth it.

edit 1b: stand corrected. looks have class this, buildingmodel, 1 code has not been posted here. wonder if buildingtypes should enum part of buildingmodel class.


edit 2
more swing-mvc related answers with code, please check out these links:


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -