Presentation is loading. Please wait.

Presentation is loading. Please wait.

BI-SW Training day 18.01.2012. Outline What is the EDT? Why should I care about it? How do I treat GUI objects properly? SwingWorker and its advantages.

Similar presentations


Presentation on theme: "BI-SW Training day 18.01.2012. Outline What is the EDT? Why should I care about it? How do I treat GUI objects properly? SwingWorker and its advantages."— Presentation transcript:

1 BI-SW Training day 18.01.2012

2 Outline What is the EDT? Why should I care about it? How do I treat GUI objects properly? SwingWorker and its advantages

3 Event Dispatching Thread (EDT) A dedicated Java background thread Looping thread that handles events: Mouse/keyboard input (  actionPerformed() ) Redrawing GUI Refresh GUI repaint() revalidate() Mouse Input Keyboard Input Event Handling Loop

4 GUI Objects and EDT Most of AWT and Swing objects are not thread safe: Cause of common bugs and artifacts GUI objects should only be created and accessed in the EDT Examples of what can happen if not done so…

5 Moving GUI code into EDT Use following methods to invoke piece of code in EDT: SwingUtilities.invokeLater(…) (asynchronous) SwingUtilities.invokeAndWait(…) (synchronous) If not sure whether something is running in EDT: SwingUtilities.isEventDispatchThread()

6 Moving GUI code into EDT Moving GUI code into the EDT solves common bugs Ok, so why don’t we just run everything in the EDT? Let’s try it

7 Moving GUI code into EDT Moving GUI code into the EDT solves common bugs Ok, so why don’t we just run everything in the EDT? Let’s try it Because the GUI would become unresponsive

8 SwingWorker Utility class for background tasks in GUIs For example: A task executed after a button click Higher level than a normal thread Offers convenience methods Takes care of issues related to concurrency Runs according methods in EDT

9 SwingWorker User needs to provide: doInBackground() Optional methods: Sending result of a task to GUI: done()/get(…) [to retrieve result] Provide intermediate task status to GUI: publish()/process(…) [intermediate results]

10 SwingWorker Cancellation of task possible cancel(…)/isCancelled() PropertyChangeListener to communicate with other threads: get/setProgress(), [0…100] getState(), [Pending|Started|Done]

11 Conclusion Event Dispatching Thread Can cause non-deterministic bugs Try to be aware of it and how it works Use invokeLater()/invokeAndWait() SwingWorkers Consider them when you are writing GUI background tasks


Download ppt "BI-SW Training day 18.01.2012. Outline What is the EDT? Why should I care about it? How do I treat GUI objects properly? SwingWorker and its advantages."

Similar presentations


Ads by Google