public void Model {
private SwingPropertyChangeSupport spcs;
public void updatedb();
public void addPropertyChangeListener(PropertyChangeListener listener)
}
public void Gui {
private JFrame view;
Gui() {
view = new JFrame();
init();// for initilizing ui components
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
view.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
void addListener(ActionListener a);
void init();
void updateGUI();
}
public void Controller implements ActionListener,PropertyChangeListener {
Model m;
Gui g;
public void PropertyChange(PropertyChangeEvent e);
public void actionPerformed(ActionEvent e)
Controller(Model m,Gui g){}
}
What is the correct of way of passing messages between 3 classes MVC message passsing Image How to extend it for multiple controllers and views?I have read SO answers The MVC pattern and SWING saying this implementation is not efficient?
And whether it is right to make Controller ActionListener and PropertyChangeListener?Will it make GUI sluggish when I call updatedb() inside actionperformed()?I also read about View having reference of Controller which maps gui actions to model actions. Is this way more efficient?
Which part of code should be inside EventQueue.invokeLater? Is it right to put init() function outside run()?Or Should I wrap the whole gui class inside run?
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui view = new Gui();
} catch (Exception e) {
e.printStackTrace();
}
}
});
What is the correct of way of passing messages between 3 classes MVC message passsing Image How to extend it for multiple controllers and views?I have read SO answers The MVC pattern and SWING saying this implementation is not efficient?
This comes down to needs, personally, I define contracts/interfaces for each level and provide references to each sub level. That is, the model and controller have a contract that each must meet, the view and the controller have a contract that each must meet in order to facilitate communication.
This allows for a one-to-one communication pipeline, it also decouples each layer in such away as they don't care about how the other is implemented.
Equally, you could use some kind of Observer Pattern, which allows interested parties to register themselves to an object so that they can notified when some change occurs. Typically this creates a one-way communication out of the observable object to many observers.
And whether it is right to make Controller ActionListener and PropertyChangeListener?
Personally, the controller should have as little knowledge about how the UI is physically implemented as possible. If your viewer contract provides ActionListener
and/or PropertyChangeListener
support, that's okay, but if you're relying on the underlying implementation objects, that's probably not a good idea (IMHO) - it increases the coupling.
Will it make GUI sluggish when I call updatedb() inside actionperformed()?
Any action you take while within the context of the GUI's main thread (the Event Dispatching Thread in Swing for example), will prevent the UI from responding to new events within its event queue. The longer you prevent the GUI's main thread from running, the less responsive your UI will be. Short answer, don't block the GUI's main thread with long running or blocking processes...
I also read about View having reference of Controller which maps gui actions to model actions. Is this way more efficient?
Generally speaking the view and model shouldn't actually talk with each other, they're suppose to communicate via the controller. The view and controller need to know about each other and the model and controller need to know about each.
The amount of knowledge will depend on the requirements of the contract, for example, if you use an Observer Pattern, the observable object won't need to know anything about the observer (other then the fact that they have one or more methods that they can call)
Which part of code should be inside EventQueue.invokeLater? Is it right to put init() function outside run()?Or Should I wrap the whole gui class inside run?
Any code that creates or modifies the UI or UI components MUST be run from within the context of the GUIs main thread.
You could also take a look at Implementing the Controller part of MVC in Java Swing and Java and GUI - Where do ActionListeners belong according to MVC pattern? for more discussions on MVC in Swing...