Gui Not Working After Rewriting to MVC

Converting app to MVC and running it in both console and gui

The current separation looks good. Here are some pointers for implementing the console view:

  • expose the controller's actions in a UI independent way. Such as an abstract Action class. The view then invokes the action in response to UI gestures. This keeps the controller independent of the view implementation, and allows the same controller to be used by multiple views.

    • add notifications from the model of changes, to keep views in sync.

The Console View can then read standard input, and write status to standard output by querying the model, and invoke functions using the Actions exposed by the controller.

A good test of MVC is creating two views on the same model and controller - both should work correctly and update from changes from the other.

basic MVC pattern and GUI

I think it's first of all a question of style and personal preference. It also depends on what your application should do and how.

If your JPanels all are very similar in a certain way it would probably make sense to extend from JPanel. For example if every panel had 10 Buttons where Button 1 does always action xyz() for its corresponding model-object (especially when this object is the same for all buttons of 1 JPanel) and Button 2 does abc() ...

If the JPanels are not strongly correlated in such a way I would place my code rather in the JFrame or in a third Object which sets the whole GUI up from outside.

It's similar with the actionlisteners. If you have very few actions I would probably go for just one controller-object for ease of use (not many files). If you have a lot of different actions I would group similar actions into one controller-object per group of actions.

That said it probably is best to start with a simple approach (YAGNI) where you do everything from the JFrame / third object and have one actionlistener and then refactor when you feel that splitting things up gives you a cleaner or more flexible design.

How to Start a Java MVC Application With a Swing GUI

All the code that creates or interacts with Swing components must run on the event dispatch thread. So the second form of your code, that is the bellow code is correct.

`SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model = new Model();
View view = new View(model);
new Controller(model, view);
}
});`

The reason for all UI code that must run through EDT or worker thread is to avoid Multithreaded problem. You may see many swing programs may not init code in EDT. This is perfectly ok. But when your swing gets complecated, then there is probability for error. my self in simple swing apps lanching from main thread, i did not face dead locks are race conditions. Quick tasks uses EDT and long running tasks uses worker threads.
Here is link on multithreaded problem on ui

Please let me know if i went wrong

building swing gui using mvc pattern with observer pattern

An implementation of MVC explained along with how an observer pattern might work:

Up-to-date Swing MVC example + Question

Some guidelines to follow while using Swing:

GUI guidelines for swing



Related Topics



Leave a reply



Submit