Streaming Data Between Threads Using Pipes

Chapter 9 - Threads and Swing

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

When invokeAndWait() and invokeLater() Are Not Needed
It is not always necessary to use invokeAndWait() and invokeLater() to interact with Swing components. Any thread can safely interact with the components before they have been added to a visible container. You have seen this already in the examples: The main thread constructs the GUI and then invokes setVisible() . After the components have been drawn to the screen, only the event thread should make further changes to their appearance.
There are a couple of exceptions to this restriction. The adding and removing of event listeners can safely be done by any thread at any time. Also, any thread can invoke the repaint() method. The repaint() method has always worked asynchronously to put a repaint request onto the event queue. And finally, any method that explicitly indicates that it does not have to be called by the event thread is safe. The API documentation for the setText() method of JTextComponent explicitly states that setText() can be safely called by any thread. The setText() method is inherited by JTextField (a subclass of JTextComponent ), so any thread can safely invoke setText() on a JTextField component at any time.
  Tip If you arent sure whether a particular method on a Swing component can be invoked by any thread, use the invokeAndWait() or invokeLater() mechanism to be safe.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net