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
C++ Equivalent of Java'S Instanceof
Unfortunately Myapp Has Stopped. How to Solve This
Why Doesn't Recyclerview Have Onitemclicklistener()
How to Read a Text File in Android
How to Get the Sharedpreferences from a Preferenceactivity in Android
How to Create Android Facebook Key Hash
Relation Between Memory Host and Memory Arguments Xms and Xmx from Java
Should I Avoid the Use of Set(Preferred|Maximum|Minimum)Size Methods in Java Swing
Understanding Checked VS Unchecked Exceptions in Java
Why Is Char[] Preferred Over String For Passwords
Error Java.Lang.Outofmemoryerror: Gc Overhead Limit Exceeded
How to Concatenate Two Arrays in Java
How to Read Input from the Console Using the Scanner Class in Java
:: (Double Colon) Operator in Java 8
How to Round Up the Result of Integer Division
How to Fix 'Android.Os.Networkonmainthread
exception'
How to Write Files to External Public Storage in Android So That They Are Visible from Windows