Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate a JMenuBar in an MVC architecture in Java?

I'm using this tutorial to build an application using the MVC architecture : http://www.oracle.com/technetwork/articles/javase/index-142890.html. But I'm not sure how or where should I put the code to build and add actionListener of a JMenuBar inside of it.

Furthermore, the book Object-Oriented Design & Patterns by Cay Horstmann say "The controller may process mouse and keyboard events from the windowing system, or it may contain user interface elements such as buttons and menus." Should I follow this advice, if yes, how should I implement it ? How to add it to the JFrame that is in my Main class ?

like image 606
JPB Avatar asked May 11 '11 15:05

JPB


1 Answers

As suggested in How to Use Actions, Action is a convenient way to encapsulate this. Moreover, Action "can be used to separate functionality and state from a component."

Addendum: In this very simple example, the model is a File representing a directory in a file system, and the view is a JLabel that listens for the actionPerformed(). The encapsulation afforded by Action ensures that each menu item and tool bar button produce the same result. The approach is emblematic of Swing's separable model architecture.

like image 143
trashgod Avatar answered Nov 05 '22 21:11

trashgod