user1577708
user1577708

Reputation: 169

swing Mvc Model

I try to create one MVC Application with Swing. And I am confused with the implementation and how things should be. What i mean is:

  1. I have the Gui which is the view all the logic send to a class named controller and I have a model where I have model properties.(I have read MVC is like that)

  2. I create some Random codes in view with input of how many codes I want and transfer this with ActionListener to a Class Named Controller. The Random codes generated with a button on the controller class in a method . The random codes are generated and then i want to save them on a database. I am confused how to save the generated codes on the database. Should i create a method in the Class Named Controller so that i can save them from there? Or another different class with save update find.......... methods? If Yes then why I have create the Model class with Model properties? And how can I use the Model class. what is only left to understand how to use the Model class if I have to use it or if I just have to have this class so that is there. What is the use of Model class if it's only to be there with the properties and do save some where else? What approach is usually used so that I am ok with MVC pattern? Am I confused? Any help I forget to tell that I use Hibernate. thanks ps. I have also read this http://java.sun.com/products/jfc/tsc/articles/architecture/ but i didn't understand it.

    public class code(){// this is the Model
        private int i;
        public void setter(int i){
            this.i=i;
        }
    
        public int getter(){
            return i;
        }
    
        public String generateStringInt() {
            return new BigInteger(190, random).toString(32);
        }
    
        // what ever i want to create with the int i variable i will do it on this class?
        ///then i will pass it on the controller to sent it on the view  
        //OR save if i want to save it.?Is this way of thinking right?
        //Or is there any other way to do it ?
        /// if i have a Button and press it from the View it will generate this method?or
        // i have to do it else?
        public String generateStringInt() {
            return new BigInteger(190, random).toString(32);
        }
    
     }
    
     //then if i want to save i can just do it like 
     //session.save(object) or is there any other way?
    

is it better now ? Thanks

Upvotes: 3

Views: 544

Answers (2)

GiorgoCH
GiorgoCH

Reputation: 194

Hi there i have the same problem with here is a tutorial very simple to use and understand what's all about

http://www.leepoint.net/notes-java/GUI/structure/ui-model-communication.html

then this

http://www.leepoint.net/notes-java/GUI/structure/30presentation-model.html this is the best approach i think.

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Let me break this for you....

Model - The Business Logic and Data

View - The Display of the Output of the Model

Controller - On which the action is done.

Swing in java is based on MVC. Its also known as PLAF (Pluggable Look and Feel)

The advantage of using this MVC architecture is that, you can keep the same model and keep changing the Views.

Eg:

Have a model which runs your Calculator program.

Now take this model and then either use a Swing or JSP to reflect the Output, one for desktop another for web respectively.

In case of Swing Applications the Sequence of MVC is this way....

Action is done on the Controller
Controller tells the Model about it
Model make necessary changes, according to the Action
Controller informs the change in state of Model to the View
View will update itself.

In case of Web Applications the Sequence of MVC is this way....

Action is done on the Controller
Controller tells the Model about it
Model make necessary changes, according to the Action
Now Controller informs View and Also make the Changes reflect in View

Upvotes: 5

Related Questions