Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement MVC in Java with Swing?

If you would like more details please let me know, or refer to the last lines of this question. I've already read a lot and I feel I'm turning something simple into something complicated and I still get stuck here and there, so maybe you can help me in those very specific points.

I'm using Netbeans IDE 7 and JDK 7, and no frameworks. The first Window is a JFrame and all other windows are JDialogs with modal=true.

Questions:

  1. How do I correctly implement the MVC pattern with swing? From the ideas bellow Which one is better: (A) or (B)? Or maybe another one... Why is it better?

    (A) Main:

    MyModel model
    MyView view(model)
    

    MyView:

    MyController(this, model)
    

    (B)
    Main:

    MyModel model
    MyView View
    MyController controller(view, model)
    
  2. when I click jbutton1 in MainFrame I need it to open the SettingsFrame for editing settings. where should I instantiate the View, the Model and the Controller of the SettingsFrame? In MainFrame Controller?

  3. In terms of MVC organization and implementation, how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)? Should I create empty classes for them?

    a. The implementation of a TrayIcon
    b. A URL connection class (an HttpsUrlConnection which will update data in the main jframe and also upload/download files)
    c. A Directory Monitor (which will update data in the main jframe and also use the urlconnection to download a file)
    d. My own implementation of TableModel
    e. json
    
  4. How to correctly keep and use an object with settings through the whole application? I will need it's information in different places (Views, Models, Controllers) but it might be altered by user during the runtime). Is it a good idea to make this model a singleton?

  5. What should I do when:

    a. View needs some data from the Model? 
    What I'm doing: using the reference of Model which I keep in the View
    b. View needs some data from the Controller?
    What I'm doing: using the reference of Controller which I keep in the View
    c. Model needs some data from the Controller?
    Still didn't happen but I have no idea how to do correctly
    d. Model needs some data from the View?
    What I'm doing: pulling all my hair from my head...
    e. Controller needs some data from the View?
    What I'm doing: using the reference of the View which I keep in the Controller
    f. Controller needs some data from the Model?
    What I'm doing: using the reference of the Model which I keep in the Controller
    g. One of FooModel, FooView or FooController needs data from one of BarModel, BarView or BarController?
    What I'm doing: thinking of jumping from the highest building...
    
  6. Any hints on how to know if I implemented MVC correctly? Should I process massive data in Model or Controller?

  7. I'm also using a DAO, what I'm doing is: my model has a

    ArrayList MyModel load()

    method which creates an instance of the DAO and returns the ArrayList of Models returned by the DAO, and then sometimes I process this ArrayList of Models in the Model and sometimes I allow the Controller to process it. Is this a good practice or is there a better way? By Process I mean: iterate through the ArrayList and get the data from the models.

  8. I Have a PasswordCheck jDialog to restrict access to some Views. How can I reuse it in terms of MVC, so that I can use the same PasswordCheck dialog for allowing/restricting access to different Views without doing a mess in the code?

  9. Any other tips, hints, ideas, suggestions?

Context: I'm required to develop a Java Swing MVC software in a short time, although by default I'm not a Java developer and not so used to implement the MVC pattern, specially in Java (I get the idea but sometimes it lacks me knowledge to implement the relationship between classes). The applications is basically a monitor for local/online files with a JTable in the main frame to show this data. I'm using the new WatchService API to keep track of the local files and saving their info in a h2 database using a DAO, and them reload this data in the main frame jtable. I must also notify the user about new files (which for I'm using TrayIcon). For the online files monitoring/uploading/downloading I'm using HttpsUrlConnection and json. It may also allow Settings customization.

Thanks in advance for you time and help.

like image 979
dcr Avatar asked Jun 07 '12 12:06

dcr


People also ask

How MVC is used in Java Swing?

Swing architecture is rooted in the model-view-controller ( MVC) design that dates back to SmallTalk . MVC architecture calls for a visual application to be broken up into three separate parts: A model that represents the data for the application. The view that is the visual representation of that data.

Does Swing follow MVC architecture?

Swing uses the model-view-controller architecture (MVC) as the fundamental design behind each of its components.

Can we use MVC in Java?

Advantages of MVC Architecture in JavaAs components have a low dependency on each other, they are easy to maintain. A model can be reused by multiple views which provides reusability of code. Adoption of MVC makes an application more expressive and easy to understand. Extending and testing of the application becomes ...

How is MVC implemented?

MVC architectural pattern follows an elementary idea – we must separate the responsibilities in any application on the following basis: Model: Handles data and business logic. View: Presents the data to the user whenever asked for. Controller: Entertains user requests and fetch necessary resources.


1 Answers

Have a look at Sun's (Oracle's) suggestions.

As one simplification, you could have each component (model, view, controller) register with a top-level application component to provide a single point of reference rather than individual references between each component (your A or B). The article I cite provides ideas for push and pull design; I'd recommend push as a more popular modern approach. Disclosure: I have experience with Java and MVC but not MVC in Swing per se.

where should I instantiate the View, the Model and the Controller of the SettingsFrame?

Sure, yes, or in a top-level application component.

how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)?

I would implement GUI-only pieces as your own GUI library. And purely algorithm/service pieces as a services library.

Should I process massive data in Model or Controller?

Data processing algorithms would fit nicely in a controller or even service library; your model should not do much processing at all beyond possibly data type conversion or validation.

How to correctly keep and use an object with settings through the whole application?

See my note on registration; a singleton may be appropriate.

like image 86
Will Avatar answered Sep 21 '22 00:09

Will