I am developing a simple swing application with a JTextArea
to show log messages from the entire application. In order to develop this application, I am using a simplified version of the MVC pattern, in which the controller is interposed between the view and the model in both directions:
- when the user interacts with a swing control, if this interaction requires access to the model (that is, it requires reading and/or writing the model), then the event raised by the swing control calls for an appropriate method of controller;
- when the view should be updated, the controller invokes one or more methods of the view.
Almost all exceptions are caught in the controller and sent to the java.util.logging.Logger
, as in the following code snippet.
public void onWriteFile() {
try {
// ...
}
catch(IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, "onWriteFile()", ex);
}
view.refresh(...);
}
Only the following exception is caught inside a custom graphical component and is sent to the logger (I took inspiration from the example in Listing 6. Robust listener invocation of Java theory and practice: Be a good (event) listener).
private synchronized void processDrawingEvent(DrawingEvent e) {
if (e == null) {
return;
}
for (Iterator<DrawingListener> i = listeners.iterator(); i.hasNext();) {
DrawingListener listener = i.next();
try {
switch (e.getStatus()) {
case DRAWING:
listener.drawing(e);
break;
case DONE:
listener.done(e);
break;
case DELETED:
listener.deleted(e);
break;
}
} catch (RuntimeException ex) {
i.remove();
Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Currently all calls to the Logger.log(...)
method are coming from the same thread. However, if this method was invoked by different threads, should I use a solution like this?