Can you explain me how a Controller can call another Controller method in a simple but still correct way?
Please provide some code!
background: I Have 2 different Controllers-View-Model and 2 libraries and they need to communicate between each other:
SettingsWindow/SettingsController/SettingsModel: Responsible for the app settings. The model is a singleton because I need the settings data all over the app;
A Library for Monitoring a directory which creates an event every time a file is created in a specific directory. The monitored dir path is defined in the SettingsModel; I'm using the Java 7 WatchService API for that;
A Library for Monitoring a webserver and download new files. The webserver address and the save directory are both defined in the SettingsModel; I'm using HttpsUrlConnection and a timer for that;
MainWindow/MainController/MainModel: Responsible for the main app window, which has a table that must be updated every time a new file is created in the monitored directory, and also everytime a file is downloaded by the above libraries.
So how do I correctly Instantiate and communicate those 4 different features?
How do their Controllers commnuicate between them, since they are all related?
How should I organize this project in terms of MVC?
PLEASE provide the basic MVC scaffold (skeleton) and show where I create the instances, where and how I call each others method. I'm new to Java, specially swing and mvc. I've read a lot, but I got stuck in this situation.
As suggested here, patterns such as model-view-controller and observer represent recurring design elements. A simple outline may illustrate the interaction of classes, but a general solution is beyond the scope of StackOverflow.
Still, it may be useful to look at how the principles might be applied. Suppose you are following the tutorial and have implemented a WatchService in a SwingWorker, as shown here. Now, suppose you want the main view, a JTable, to update itself in response to an arriving WatchEvent. You would arrange for the corresponding TableModel to register itself with the worker as a PropertyChangeListener, shown here and here. When the PropertyChangeEvent arrives, you update the TableModel and fire the appropriate event to update the JTable. The known TableModel implementations already include the required methods.
A similar approach would be used to update the table in response to data arriving from the web.
For settings, consider java.util.Preferences, mentioned here, or javax.jnlp.BasicService, discussed here.
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