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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With