3

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

Niko
  • 26,516
  • 9
  • 93
  • 110
user1577708
  • 169
  • 1
  • 1
  • 13
  • a [sscce](http://sscce.org/) will help. – gontard Aug 24 '12 at 09:47
  • A complete tutorial is beyond the scope of this site. Please edit your question to include an [sscce](http://sscce.org/) that shows what you've tried and where you have encountered a problem. – trashgod Aug 24 '12 at 09:49

2 Answers2

5

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
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • +1 for overview. See also this related [answer](http://stackoverflow.com/a/3072979/230513). – trashgod Aug 24 '12 at 09:47
  • @ Kumar Vivek Mitra Controller tells the Model about it.That means?What the Controller tells to Model.My Model is a class with get/set. – user1577708 Aug 24 '12 at 09:48
  • @user1577708 `A Class should be Cohesive. That means the class name should be reflected according to it methods`. That points out that methods are important to class, as instance variables to an object. So your class that works as the model, Must have logic that your program is suppose to do. A class with Only getter-setter is Not a model – Kumar Vivek Mitra Aug 24 '12 at 09:52
  • @ Kumar Vivek Mitra That means that i have to implement the model logic inside model?Example if i have to generate a random number i will do that on the model class?And Then i will create a save it from the model class?Else if i want to show it to my view i will sent it to Controller or view?Should i made one example for all this? – user1577708 Aug 24 '12 at 10:23
  • @ Kumar Vivek Mitra , trashgod ,gontard Hope the code helps to explain what i mean. – user1577708 Aug 24 '12 at 10:36
  • @thrashgod the answer you posted is very good.From what i understand is that ActionListeners mouseListeners and stuuf that has to do with reacting goes to the Controller.Then Controller with actions redirects to Model and what ever logic must be done it take place there.On my case the generated Numbers should be generated in Model.Then View Takes the model result and show it .Is this all right? – user1577708 Aug 24 '12 at 10:49
  • @user1577708: A particular _model_ might export an immutable instance of a concrete subclass of `java.lang.Number`. In general, a _model_ represents data that the _view_ presents to the user. – trashgod Aug 25 '12 at 02:27
1

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.

GiorgoCH
  • 194
  • 10