0

How to develop a Simple MVC Architecture Java Application

Posted by Nantharupan on 11:49 PM in , , ,
There are a lot of architecture in the Software industry. Basically features of most  architecture  is reduce the coupling between View and Logic. By using that we can change the view when ever we want and keep the logic same.

Some of the Architectures are

1. MVC- Model View Controller
2. MVVM- Model View ViewModel


I have been working in MVC architecture in .NET web application and MVVM  architecture  in WPF desktop application, and Mono Develop Mobile application.

When i try to develop java based desktop application, i tried to do the MVC architecture using help from internet.

Basics of MVC













M-Model
V-View
C-Controller

Model Class

Which will contain the logic of the application and data base connection and manipulation.
/*
*program: Adding Two Numbers Using MVC
*author: rupanl.blogspot.in
*date: 10-May-2013
*/
public class Login_Model {
public int add(String x,String y)
{
int a=Integer.parseInt(x);
int b=Integer.parseInt(y);
int result=a+b;
return (result);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub



View Class

View will be contains the User Interface part only.
public class Login_View extends JFrame {
Container con;
JButton addButton;
JTextField textField1,textField2,textFieldresult;
JLabel first,second,result;
public Login_View(Login_Model model) {
con=getContentPane();
setTitle("MVC ADD");
setLayout(new FlowLayout());
setSize(220,200);
first=new JLabel("first variable");
textField1=new JTextField("",10);
second=new JLabel("second variable");
textField2=new JTextField("",8);
result=new JLabel("result");
textFieldresult=new JTextField("",13);
addButton=new JButton("add");
con.add(first);
con.add(textField1);
con.add(second);
con.add(textField2);
con.add(result);
con.add(textFieldresult);
con.add(addButton);
setVisible(true);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Controller

Controller will do the communication part between View and Model
<
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login_Controller implements ActionListener
{
Login_Model model;
Login_View view;
public Login_Controller(Login_View v,Login_Model m)
{
model=m;
view=v;
actionListenerMeth(this);//add action listener to the Button
}
@Override
public void actionPerformed(ActionEvent arg0) {
try
{
//retrieve the input from View
String a=view.textField1.getText();
String b=view.textField2.getText();
//call add method
int result=model.add(a,b);
//display result in View(UI)
view.textFieldresult.setText(""+result);
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public void actionListenerMeth(ActionListener ae)
{
view.addButton.addActionListener(ae);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Main Method


public class GlobalMVC {
public static void main(String args[])
{
Login_Model m=new Login_Model();
Login_View v= new Login_View(m);
new Login_Controller(v,m);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

0 Comments

Post a Comment

Popular posts

Copyright © 2009 On the way All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.