Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA Swing MVC - Main Controller?

I'm having some troubles designing the architecture of an application I'm trying to develop. I'm working on JAVA, and I started working on this application because I want to deepen my overall knowledge of JAVA, architectures and patterns. I want to follow the guidelines to make a reusable, low coupled application, like it should be. The application has only one JFrame, but inside it there are several JPanels, each one representing a module of the application.

The question is: in JAVA Swing, how to implement an appropriate MVC pattern? I struggle on how to understand the way it should be done.

Should I have a main Controller class, that holds references to all the other Controllers? (I have an image to demonstrate this, here: https://docs.google.com/file/d/0B7tBdn5slIFeY2FoSmxESTREQ1k/edit?usp=sharing)

And in this case, should all the events that require changing the module that is being presented redirect to the main Controller?

Or should I just couple the JFrame with the Controllers of the application, and communicate directly with them?

Basically, I would like to know if i need to have a class that 'manages' all the others. I have already read several explanations, and different opinions, but I believe this is a little more specific.

Hope I have made myself clear (and hope as well that my explanation is better than my drawing :)).

EDIT: a sample of the application usage:

  • One (an only one) JFrame throughout all the lifecycle of the application;
  • the menu will be on the left side, as in BorderLayout.WEST;
  • the current module of the application will be in the center, as in BorderLayout.CENTER;
  • when the user presses one button of the menu, the corresponding module is loaded into the BorderLayout.CENTER;

Should the menu (View) have it's own Controller, and this Controller communicate with the JFrame? And the JFrame load the new module into it's Layout? Or should the JFrame have its own Controller (or Model, as Gilbert Le Blanc said)?

I know this may seem to specific, or easy to understand, but everytime I think of an desktop application, I struggle to understand this.

like image 685
Pedro Duarte Avatar asked Mar 28 '13 22:03

Pedro Duarte


People also ask

Is Java Swing still used in 2022?

JavaFX new fixes will continue to be supported on Java SE 8 through March 2022 and removed from Java SE 11. Swing and AWT will continue to be supported on Java SE 8 through at least March 2025, and on Java SE 11 (18.9 LTS) through at least September 2026.

What are MVC explain how MVC is used in the 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.

What is controller in MVC in Java?

MVC stands for Model View and Controller. It is a design pattern that separates the business logic, presentation logic and data. Controller acts as an interface between View and Model. Controller intercepts all the incoming requests. Model represents the state of the application i.e. data.


1 Answers

When you have an application with a GUI, the GUI model becomes the application view. The application interacts with the GUI through the GUI model.

Or should I just couple the JFrame with the Controllers of the application, and communicate directly with them?

This is what I've done. I've packaged the controller classes together, but I've never created one main controller class.

I keep the GUI controller classes in a separate package from any other application controller classes, like the data access objects.

I usually put each JPanel in its own class, but I wouldn't call that a requirement. The JFrame has its own class, although the instance of the JFrame and the instance of the GUI model are passed to almost all of the GUI components. This makes menu actions possible.

This Traffic Signal GUI article goes over the basics of how to code a very simple GUI.

Edited to respond to the changes in the question.

The GUI controller is separate from the GUI model. The GUI model contains all of the data elements that make up your GUI. Strings for JTextFields, DefaultTableModels for JTables.

Based on your application design, I'd recommend that you create a Java class for every JPanel that you want to put in the center of your application. Your JFrame will control which JPanel is displayed, based on the menu. I'd also suggest that you look at the JTabbedPane which uses a different user interface to accomplish the task of choosing which panel to work with.

Assuming you're going with the menu on the left, each menu option (toggling JButton?) will have it's own controller method or class. These controllers have to have an instance of the JFrame so the controller can call the method in the JFrame class that puts the appropriate panel in the center of the display. The controller decides which method to call, but the methods themselves are a part of the JFrame class.

I've been talking about JFrame and JPanel classes. It's important that you use composition rather than inheritance to build these classes. The JFrame class contains a JFrame. It does not extend JFrame. The only time you extend a Swing component is when you want to override a component method.

like image 188
Gilbert Le Blanc Avatar answered Oct 23 '22 10:10

Gilbert Le Blanc