MVC Architecture injected in Both JTable of Swings and DataGrid of .net

       Everything boils down to the concept. Irrespective of what language or Technology you are using to implement your Business Process…. I think the JTable of Swings and DataGrid of .net is a typical examples to prove it.

         Before i move further, In brief MVC(Model View Controller) is an Architecture,  where in the the Business Logic resides in the Model, n the User interaction ie JSPs, JSFS,aspx… will be at the View .. and controller orchestrates the interactions of  Model and View..  for More details about MVC Read Here..

       I like to quote the similiarities b/w the Java Swings JTable and .net’s Datagrid

           JTable is one of the most powerful, customizable, and difficult pieces of the entire API.  With the exception of JTree, no other object within the API can be used in so many ways.

     In both JTable and DataGrid , Model and Controller  not segregated unlike Struts Framework. In JTable for just displaying the data which is static, we use the simple arrays  or vectors, usually we ust array for storing the name of the columns like String[] cols={“col1″,”col2″,”col3”}. and the data to be displayed for each columns can be stored in the vectors.

        In the sophisticated JTable where in the data can be updated ,deleted , fetched, we write the  user defined TableModel which extends the AbstractTableModel ,and overrides the different methods like getColumncount(), getColumnNames(),getValueAt(int row,int col),getRowCount(),and Constructors which is shown as follows:-

     public void mytableModel extends AbstractTableModel{

              public mytableModel(){ ….}

              public mytableModel(List l){…..}

              public int getColumnCount(){ return 0;}             

              public int getRowCount{return 0;}

              public Object getValueAt(int row, int col) {
                return null;

             }

 }

     We, use this TableModel for our JTable. So now why TableModel.. whats the use of this.. As i was talking about MVC this Table acts as the Controller. It is from this TableModel the Data can be manupulated.   When ever the User wants to accesst the data from the JTable say 2 Row, 3rd Column data, it actually doesnt get  the reference of the data from the JTable’s View ie the presentation Layer which shows in rows, columns  on the GUI. It actually fetches from the this Controller,the method getValueAt(int row, int col)  will return the data object from the datasource. To put in simple words the Presentation layer of the JTable just shows the data which is there in the data Source.  it can be ArrayList,Vector etc. when the data is to be retrieved.  The  required  col and row value is passed to the method getValueat(int row, int col ) which queries the datasource and fetches the data object. when ever the data is updated or changed the data in the Datasource is updated and the fireTableDataChanged()  is called to notify the Listeners that the data is changed it he Datasource and it needs to refresh the same int he Presentation Layer.

         Now where is M-> Model for the JTable? The Model of the JTable is another Class or a bean which fetches and updates the data  as follows:-

              class modelClass{

                   protected String Name;
                   protected String Designation;
                   public AudioRecord() {
                           Name = “”;
                           Designation = “”;
                   }

                  public String getName() {
                  return Name;
                 }

                 public void setName(String nam) {
                 this.Name = nam;
                }

                public String getDesignation() {
                return Designation;
                }

               public void setDesignation(String desig) {
               this.Designation = desig;
             }

    }

               In the above Class ie Model Class, I have just used the String Variables.. the datasource as i mentioned can be anything like ArrayList, Vector.

    So that was the JTable explained from MVC perspective.  My aim was to just highlight the MVC Concept so the actual coding for the JTable is omitted. 

   DataGrid of .Net

     So that was about JTable. Now lets analyse how the famous Datagrid of the .net can be explained wrt to MVC architecture.

          As we all know there is no much hitches about the .Net coding as  much of the so called dirty work is already done by MS(for which we pay!)  has a simple coding as follows:-

           System.Web.UI.WebControls.DataGrid dgrid;           

          dgrid.DataSource = dsource;

          // Here dsourcethe dataSource          

          dgrid.DataBind();

         // This will bind the data from the datasource to the Viewer or the Presentation Layer of the DataGrid.             

         Likewise JTable even there the data source can be anything.. it can be an ArrayList, Vector, Dataset etc…  when we say dgrid.DataSource=dsource,  this would be actually establishing the relationship b/w the Presentation Layer and the Model ie the datasource which is  hidden where as in JTable the  customTableModel’s constructor take it in as the parameter.   In the .Net class similiar to customTableModel is hidden , Developer need not worry about the coding about that class as its done in the framework. Secondly when the dgrid.DataBind() method is called,  the data in the DataGrid gets refreshed similiar to the JTable’s Listener fireTableDataChanged();.             

           Again when the particular data needs to be retrieved the Datagrid refers to the Datasource and again the Model class implementation which  i have explained above wrt modelClass is readily given in the .net framework, so there is no need to code for it.              This proves that Concept of MVC is injected in both JTable of JFC(Java Swings) and the Datgrid of the .Net framework.

   Ref web sources

                  http://www.javalobby.org/articles/jtable/

                  http://www.awprofessional.com/articles/article.asp?p=332278&rl=1

5 Comments

  1. >> As we all know there is no much hitches about the .Net coding as much of the so called dirty work is already done by MS(for which we pay!)

    I didnt know that you paid Microsoft to use .NET/the datagrid

  2. @ sunil I wasnt talking about any particular individual who pay for the .net or i never meant that you gotta pay for each N every component..
    All i was trying to convey is the implementation difference b/w two Technologies..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s