11147

Where should I invoke my swingworker in a Java MVC pattern

I have a program that is basically set up just like the one in this MVC example: http://www.leepoint.net/notes-java/GUI/structure/40mvc.html

In my program there is a process which takes quite a bit of time which freezes my GUI. I want to GUI to continuously update while the process is running. To do this I believe I need to use a SwingWorker. I don't know where in my MVC pattern I should be invoking this SwingWorker.

My thinking is that I should be running it in the MultiplyListener actionlistener of the controller. Would this be correct?

Answer1:

In this case, the model is a mathematical operation that evolves over time, perhaps by iteration. Clearly, the worker belongs in the model, as shown here. The setProgress() method will notify any PropertyChangeListener, and process() can notify any other listening view, as shown here.

Addendum: In the second example, the worker updates a different model: the chart's dataset named collection. The chart, in turn, listens to the dataset and updates itself in response to the change.

In the example cited, the controller installs action listeners on behalf of the model and view. In this related example, the GUI action listeners are local to their respective views. In either case, Action would be a suitable choice for encapsulation. The model notifies its observers when its internal state changes in response to user initiated actions.

Answer2:

I think that your SwingWorker belongs in the Control and I'll tell you why. I feel that the Model should be as View agnostic as possible and should be created with the idea that it can be used with different views and controls, and even with a completely different GUI library, if desired. For instance, you are currently wanting to use the Model in a Swing GUI, but what if later you want to use it in an Android application? In order to allow it to be used with as few modifications as possible, most code that is Swing-specific, such as a SwingWorker should reside in the Control or View.

For example, please check out this answer.

Recommend

  • Check if one of variables is set to None
  • icon in JButton is not shown at the running of the program, what could be?
  • Retrieving the value of the selected item in a SelectOneMenu List
  • Java Swing: Set starting time on timer and loop it
  • adding an action listener to a JComboBox
  • Where should I invoke my swingworker in a Java MVC pattern
  • Java - Enforce TextField Format - UX - 00:00:00;00
  • Export Java applet to .jar
  • How to use Swing Timer ActionListener
  • Result discrepancy between cv.MinAreaRect2 and ArcGIS (GIS software) . Possible bug?
  • JSF commandLink with image
  • iPhone set interfaceorientation programmatically
  • Primefaces Dialog Framework - Open dialog - close it - open another dialog
  • Is a single constant value considered an expression?
  • Two different SelectItems return single selected value
  • Getting Coordinates of a Point in a circle
  • Exception seems to be thrown repeatedly when debugging
  • Android FileProvider delete file
  • How to properly fetch single cells in pandas: loc[index,column] VS get_value(index,column)
  • Python : How to center Label in tkinter window
  • Celery + Redis - .get() hangs indefinitely after running smoothly for ~70 hours
  • why lock_guard can get an already locked mutex by unique_lock?
  • Greek letters in a GUI - PYTHON
  • Client-Side: Accessing Windows Azure Drive?
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Refactoring advice: maps to POJOs
  • Iterate twice through a DataReader
  • Can a Collections.shuffle be considered equivalent to a series of Randoms?
  • What is the default HTTP verb in WebApi ? GET or POST?
  • TFS 2015 - Waiting for an agent to be requested
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • calculate gradient output for Theta update rule
  • Object and struct member access and address offset calculation
  • MongoDb aggregation
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Update CALayer sublayers immediately
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?