|
|
Summary
To fully understand and exploit the power of the JFC, some programming techniques need to be understood. This chapter addressed some of those issues. It was seen that the JFC is a single-threaded toolkit. Due to the multithreaded nature of Java, this may have come as a surprise to many programmers. The JFC developers decided that most user interface code was inherently single threaded. Thus the extra complexity and performance penalty for making the Swing toolkit multithreaded were not worth the effort. This decision is being hotly debated, and probably will be for as long as the toolkit is used. However, the fact is that this version of the toolkit is single threaded and using it in a multithreaded application requires special programming techniques. These techniques where discussed in this chapter. For most types of updates of a Swing component from a thread other than the event-dispatch thread, the invokeLater or invokeAndWait method must be used. These methods place a Runnable object in the event-dispatch threads queue. This ensures that when the Runnable objects run() method is invoked, it will be in the event-dispatch thread.
For reccurring operations, the JFC provides the Timer class that will fire an ActionEvent in the event-dispatch thread. Thus the listeners actionPerformed method is free to update Swing components.
This chapter investigated the RepaintManager contained in the JFC. Understanding this class, or even providing your own implementation of a RepaintManager, can enhance the efficiency of painting JFC applications.
Finally, this chapter investigated Actions. It was shown how an Action could be used to encapsulate a piece of application functionality and store data associated with that functionality. The associated data can consist of a String used in a menu representation of the Action or an Icon to represent the Action on a toolbar, to name a few of the standard data items contained in an Action. Designing applications that contain Actions allows the user interface to be separated from the functionality of the application. This gives the programmer more flexibility when creating the user interface for the application. It also allows the user interface and the applications functionality to change independently of one another.
|