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.

Slides:



Advertisements
Similar presentations
Review Text Material Covered –Culwin: Chapter 8 threads –Johnson: Chapters 9, 10, 1 through p.44 –Swing: Chapter 1-15 Lessons Learned –Concepts of concurrency/synchronization.
Advertisements

Executional Architecture
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
Graphic User Interfaces Layout Managers Event Handling.
Java Applets- Using SwingWorker Dave Price and Chris Loftus Computer Science Department University of Wales, Aberystwyth.
C HAPTER 12 A Very Graphic Story. O VERVIEW Simple GUI Getting User Input Creating a Listener ActionEvents Displaying Graphics Drawing Multiple Buttons.
Events and Interrupts. Overview  What is an Event?  Examples of Events  Polling  Interrupts  Sample Timer Interrupt example.
Threads and GUIs Confession: I’ve never liked GUI programming very much – so I haven’t done a lot of it  I don’t like the visual design aspects  But.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
Software design and development Marcus Hunt. Application and limits of procedural programming Procedural programming is a powerful language, typically.
Swing part-one Eriq Muhammad Adams J
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Unit 20: Event Driven Programming
CS 0004 –Lecture 1 Wednesday, Jan 5 th, 2011 Roxana Gheorghiu.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Welcome to CIS 083 ! Events CIS 068.
Event Driven Programming
CS12420 – Swing and threads Lynda Thomas
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
Graphics in Java Starring: NetBeans Co-Starring: Java.awt.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Automated GUI testing How to test an interactive application automatically?
CS Lecture 00 Swing overview and introduction Lynda Thomas
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Concurrent Programming and Threads Threads Blocking a User Interface.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Keeping your Swing Applications Responsive using FoxTrot and Friends Rob Ratcliff.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Before class… 下禮拜一交 Proposal – 至多四人一組, 同組同分 – 請勿超過一張 A4 評分標準 創意 技術難度 實用性 User-defined 完整性.
Don’t Disconnect Me! The challenges of building offline-enabled web apps Matthias Oßwald,
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
Review_6 AWT, Swing, ActionListener, and Graphics.
CSC Multiprocessor Programming, Spring, 2011 Chapter 9 – GUI Applications Dr. Dale E. Parson, week 11.
Event Handling Tonga Institute of Higher Education.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Java Thread Programming
Events and Event Handling
Reactive Android Development
Event Loops and GUI Intro2CS – weeks
Event loops 16-Jun-18.
CSC Multiprocessor Programming, Spring, 2012
CSE 331 Software Design & Implementation
Unit 20: Event Driven Programming
Lecture 28 Concurrent, Responsive GUIs
CSE 331 Software Design and Implementation
Ellen Walker Hiram College
Event Driven Programming
CSE 331 Software Design and Implementation
Event loops.
CS371m - Mobile Computing Responsiveness.
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Event loops 8-Apr-19.
Tonga Institute of Higher Education
Using threads for long running tasks.
Event loops.
CSE 331 Software Design & Implementation
Event loops.
Ch 3.
Event loops 19-Aug-19.
Presentation transcript:

BI-SW Training day

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

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

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…

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()

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

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

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

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]

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

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