java - The MVC pattern and SWING -


one of design patterns find difficult real grasp of in "real swing life" mvc pattern. i've been through quite few of posts @ site discuss pattern, still not feel have clear understanding of how take advantage of pattern in (java swing) application.

let's have jframe contains table, couple of text fields , few buttons. use tablemodel "bridge" jtable underlying data model. however, functions responsible clearing fields, validating fields, locking fields along button actions go directly in jframe. however, doesn't mix controller , view of pattern?

as far can see, manage mvc pattern "correctly" implemented when looking @ jtable (and model), things muddy when @ entire jframe whole.

i'd hear how others go regard this. how go when need display table, couple of fields , buttons user (using mvc pattern)?

a book i'd highly recommend mvc in swing head first design patterns freeman , freeman. have highly comprehensive explanation of mvc.

brief summary

  1. you’re user — interact view. controller takes actions , interprets them. if click on button, it’s controller’s job figure out means , how model should manipulated based on action.
  2. the controller asks model change state. when controller receives action view, may need tell view change result. example, controller enable or disable buttons or menu items in interface.
  3. the controller may ask view change. when changes in model, based either on action took (like clicking button) or other internal change (like next song in playlist has started), model notifies view state has changed.
  4. the model notifies view when state has changed. when changes in model, based either on action took (like clicking button) or other internal change (like next song in playlist has started), model notifies view state has changed.
  5. the view asks model state. view gets state displays directly model. instance, when model notifies view new song has started playing, view requests song name model , displays it. view might ask model state result of controller requesting change in view.

enter image description here source (in case you're wondering "creamy controller" is, think of oreo cookie, controller being creamy center, view being top biscuit , model being bottom biscuit.)

um, in case you're interested, download entertaining song mvc pattern here!

one issue may face swing programming involves amalgamating swingworker , eventdispatch thread mvc pattern. depending on program, view or controller might have extend swingworker , override doinbackground() method resource intensive logic placed. can fused typical mvc pattern, , typical of swing applications.

edit #1:

additionally, important consider mvc sort of composite of various patterns. example, model implemented using observer pattern (requiring view registered observer model) while controller might use strategy pattern.

edit #2:

i additionally answer question. should display table buttons, etc in view, implement actionlistener. in actionperformed() method, detect event , send related method in controller (remember- view holds reference controller). when button clicked, event detected view, sent controller's method, controller might directly ask view disable button or something. next, controller interact , modify model (which have getter , setter methods, , other ones register , notify observers , on). model modified, call update on registered observers (this view in case). hence, view update itself.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -