Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 28 Concurrent, Responsive GUIs

Similar presentations


Presentation on theme: "Lecture 28 Concurrent, Responsive GUIs"— Presentation transcript:

1 Lecture 28 Concurrent, Responsive GUIs
(D&D 23)

2 Summary of Previous Lecture
Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Covered common difficulties with Assignment 2 Recap of Java GUI creation AWT (Abstract Window Toolkit) Swing Object hierarchy and layout of Swing elements JFrame JPanel JComponent Model View Controller (MVC) pattern Creating a custom JTable

3 Multi-Threaded GUI Application
Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

4 Event Queue Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Event: Something of interest has happened. For example a MouseEvent (Click, Move etc.) Event Queue: Each event is added to the Event Queue to be processed. These will be processed in First In First Out order like a normal queue operates.

5 Event Loop Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Event Loop: The events that are passed into the event queue are processed by a continuously running Event Loop. The event at the front of the queue will be removed and executed by the Event Loop. This execution of logic is performed by the Event triggering its relevant Event Handler

6 Event Handler Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Event Handler: The different Event types will execute certain tasks depending on how their Event Handler is implemented.

7 Event Structure Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary What’s wrong with this design structure in the above figure?

8 Single Threaded GUI Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary The previous example was using a single thread to control the handling of all these events. One event would enter the Event Loop, and would the appropriate task would be executed by the Event Handler. Before the next Event can be taken from the Event Queue, this task must finish its execution! If our Event Handler is executing a high computation task, for example manipulating large amounts of data, our interactions with the GUI (Events) will stop being processed. How can we solve this issue?

9 Helper Thread Assignment Java Swing/ AWT Swing Elements MVC Pattern
JTable Summary

10 Helper Thread Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary When handling high computation Events, we can create a Helper thread (Worker Thread) to perform the task in the background. This will allow the thread that created the GUI to continue updating the GUI and processing Events in the Event Queue. The thread that controls the GUI and handles the Event queue is commonly known as either the ‘GUI Thread’ or more precisely the ‘Event Dispatch Thread’ (EDT). Step (5) in the previous picture shows that the results of the processed task cannot be directly passed back to the GUI. This is because Swing components are NOT thread-safe! We instead place an Event on the Event Queue to update the GUI. Using the EDT ensures that other threads cannot access swing components

11 Implementing Concurrent GUIs
Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Swing provides a framework to simplify the handling of our Event Dispatch Thread and our Helper Threads (Worker Threads). Recall the GUI Code must only be executed by the Event Dispatch Thread. We call this thread confinement. Rather than implementing our synchronization techniques, we simply ensure that only one thread (EDT) ever access the Swing Components. To implement the EDT we will make use of the SwingUtilities class . To implement our Worker Threads we will use the SwingWorker class.

12 SwingUtilities Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We can wrap the code we used in the previous lecture to create a JFrame containing a JTable inside a Runnable object. We create a new Runnable object and implement its run() method with our GUI code. We call the invokeLater() method on our Runnable to execute the creation of the GUI. Using invokeLater() will ensure that the GUI creation events are completed on the Event Dispatch Thread.

13 SwingWorker Assignment Java Swing/ AWT Swing Elements MVC Pattern
JTable Summary

14 SwingWorker Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary In this example the SwingWorker is created as an Anonymous Class (L16). We are only using this class once, so we define it and its important methods here. We override the doInBackground() method to indicate what task the worker thread should be completing. This is similar to override the run() method when implementing the Runnable Interface. We then call the execute() method to start the SwingWorker on a thread. Inside the doInBackground() method we are simply starting up our Ambulance runnables with an ExecutorService as shown previously. This sounds very similar to simply extending Thread or implementing Runnable… Why do we use SwingWorker instead?

15 SwingWorker Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary The SwingWorker class provides us with a set of methods which make the passing of data to the Event Dispatch Thread much easier. While we could perform the same task by using only an ExecutorService and implementing the Runnable task, it would add more complexity. Don’t reinvent the wheel! Our previous example did not update the JTable while the simulation was running. This is because no data was being passed from the Model to the View. To do this we can use the SwingWorker publish() method and implement its process() method to handle the data that is published.

16 SwingWorker Assignment Java Swing/ AWT Swing Elements MVC Pattern
JTable Summary

17 Problems have been fixed!
We are creating our GUI on the main thread. If there are also calculations being done on this thread, it will make the GUI slow and unresponsive! We have fixed this by using SwingUtilities to handle the Swing Components on the Event Dispatch Thread. This also means no other threads can access the Swing components, which we call ‘Thread Confinement’ While our ‘Model’ is being changed while the simulation is running, there is no communication between our ‘Model’ and ‘View’. When we click on data, or arrange the columns, we notice that some data is updated. This is because we are Notifying the View to update. How can we get this to happen when the data is changed? We fixed this by using a SwingWorker to perform background tasks. This lets us to call the publish() method to call our overridden process() method which informs the View to update Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

18 Summary Multi-threaded GUI Structure Event Dispatch Thread (EDT)
Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Multi-threaded GUI Structure Event Dispatch Thread (EDT) Thread Confinement Responsiveness Helper/Worker Threads SwingUtilities Class invokeLater() EDT SwingWorker Class doInBackground() publish() process()


Download ppt "Lecture 28 Concurrent, Responsive GUIs"

Similar presentations


Ads by Google