What is the controller in Java Swing? -


i apply mvc design java application using swing in meaningful way. therefore question is, how controllers structured in java swing?

i have 2 options in mind:

  1. every component listener own class, part of controller package
  2. every component listener anonymous class inside view package delegates call class controller methods.

is both possible? question of preference, or defined?

the controller makes other half of component interface, interaction half. controller takes care of mouse , keyboard events.

in swing components jbutton etc controllers. , listener classes redirect events model have business logic

example :

main program

import javax.swing.*;  public class calcmvc {     //... create model, view, , controller.      //    created once here , passed parts     //    need them there 1 copy of each.     public static void main(string[] args) {          calcmodel      model      = new calcmodel();         calcview       view       = new calcview(model);         calccontroller controller = new calccontroller(model, view);          view.setvisible(true);     } } 

view

import java.awt.*; import javax.swing.*; import java.awt.event.*;  class calcview extends jframe {     //... constants     private static final string initial_value = "1";      //... components     private jtextfield m_userinputtf = new jtextfield(5);     private jtextfield m_totaltf     = new jtextfield(20);     private jbutton    m_multiplybtn = new jbutton("multiply");     private jbutton    m_clearbtn    = new jbutton("clear");      private calcmodel m_model;      //======================================================= constructor     /** constructor */     calcview(calcmodel model) {         //... set logic         m_model = model;         m_model.setvalue(initial_value);          //... initialize components         m_totaltf.settext(m_model.getvalue());         m_totaltf.seteditable(false);          //... layout components.               jpanel content = new jpanel();         content.setlayout(new flowlayout());         content.add(new jlabel("input"));         content.add(m_userinputtf);         content.add(m_multiplybtn);         content.add(new jlabel("total"));         content.add(m_totaltf);         content.add(m_clearbtn);          //... finalize layout         this.setcontentpane(content);         this.pack();          this.settitle("simple calc - mvc");         // window closing event should passed          // controller in real program, short example.         this.setdefaultcloseoperation(jframe.exit_on_close);     }      void reset() {         m_totaltf.settext(initial_value);     }      string getuserinput() {         return m_userinputtf.gettext();     }      void settotal(string newtotal) {         m_totaltf.settext(newtotal);     }      void showerror(string errmessage) {         joptionpane.showmessagedialog(this, errmessage);     }      void addmultiplylistener(actionlistener mal) {         m_multiplybtn.addactionlistener(mal);     }      void addclearlistener(actionlistener cal) {         m_clearbtn.addactionlistener(cal);     } } 

controller

import java.awt.event.*;  public class calccontroller {     //... controller needs interact both model , view.     private calcmodel m_model;     private calcview  m_view;      //========================================================== constructor     /** constructor */     calccontroller(calcmodel model, calcview view) {         m_model = model;         m_view  = view;          //... add listeners view.         view.addmultiplylistener(new multiplylistener());         view.addclearlistener(new clearlistener());     }       ////////////////////////////////////////// inner class multiplylistener     /** when mulitplication requested.      *  1. user input number view.      *  2. call model mulitply number.      *  3. result model.      *  4. tell view display result.      * if there error, tell view display it.      */     class multiplylistener implements actionlistener {         public void actionperformed(actionevent e) {             string userinput = "";             try {                 userinput = m_view.getuserinput();                 m_model.multiplyby(userinput);                 m_view.settotal(m_model.getvalue());              } catch (numberformatexception nfex) {                 m_view.showerror("bad input: '" + userinput + "'");             }         }     }//end inner class multiplylistener       //////////////////////////////////////////// inner class clearlistener     /**  1. reset model.      *   2. reset view.      */         class clearlistener implements actionlistener {         public void actionperformed(actionevent e) {             m_model.reset();             m_view.reset();         }     }// end inner class clearlistener } 

model

import java.math.biginteger;  public class calcmodel {     //... constants     private static final string initial_value = "0";      //... member variable defining state of calculator.     private biginteger m_total;  // total current value state.      //============================================================== constructor     /** constructor */     calcmodel() {         reset();     }      //==================================================================== reset     /** reset initial value. */     public void reset() {         m_total = new biginteger(initial_value);     }      //=============================================================== multiplyby     /** multiply current total number.     *@param operand number (as string) multiply total by.     */     public void multiplyby(string operand) {         m_total = m_total.multiply(new biginteger(operand));     }      //================================================================= setvalue     /** set total value.      *@param value new value should used calculator total.      */     public void setvalue(string value) {         m_total = new biginteger(value);     }      //================================================================= getvalue     /** return current calculator total. */     public string getvalue() {         return m_total.tostring();     } } 

Comments