1

I'm creating my first "bigger" application in Java. As MVC is only pattern I know, I decided to use it. But there's something wrong with this concept.

For example. I need an Action (or generally event) fired from 2 places (from Button in frame and MenuItem). It has to do changes in at least 2 places and in the model.

I've got some ideas, but they seem wrong:

  1. Pass the controller object to every view element, so newly created actions could use controller's methods to modify rest of the application.
  2. Make controller static (for same reasons)
  3. Make controller only model listener

Please tell me how to build it. Or give me some links to some easy to analyse applications.

Source of my project is here, if anyone wants to have a look: https://github.com/Arrvi/ColorExtractor

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Arrvi
  • 539
  • 6
  • 13
  • For a moment, I though you were talking about mvc on web applications. – Luiggi Mendoza May 18 '14 at 18:46
  • Yeah. My bad. It's just a regular swing app. – Arrvi May 18 '14 at 18:49
  • 2
    Scratch idea #2 as that's bad, very bad. Your Controller can inject the same Action into the button and the menu item. – Hovercraft Full Of Eels May 18 '14 at 20:03
  • Your controller will need access to your view. Put your method in your frame class, and call it from the JButton action listener and from the menu bar class. Read my [Qlocktwo with Java Swing](http://java-articles.info/articles/?p=555) article to see how I put a Swing MVC application together. – Gilbert Le Blanc May 19 '14 at 02:40
  • @GilbertLeBlanc Thank you [Wayback Machine](http://web.archive.org/web/20170723150710/http://java-articles.info/articles/?p=555). And thank you Gilbert. – Piovezan May 03 '20 at 18:32

1 Answers1

1

You are correct to use Action to encapsulate functionality for use by disparate components such as menus and buttons. A spectrum of examples is cited here. As regards MVC, recall that Swing uses a separable model architecture, examined here. In effect, the user is the controller, and not every interaction needs to pass through your application's controller.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045