Chapter 1. Overview of SWT and JFace
This chapter covers - The purpose of SWT and JFace
- The reasons for their creation
- How the two libraries differ from Swing
- Licensing and platform support
In March 2004, the Java Developerâs Journal announced the results of its Readersâ Choice Award for Best Java Component. More than 15,000 developers voted for one of many Java toolsets, including offerings from such established names as Oracle and Apple. But in the end, Eclipseâs Standard Widget Toolkit (SWT) won handily, just as it did in 2003. Despite their late entry into the field of Java development, Eclipse and SWT have also won awards and recognition from JavaWorld, JavaPro, and LinuxWorld.
This well-earned applause goes a long way in showing the impact these tools have made on Java development. Java programmers around the world have embraced the power and versatility of SWT and JFace, deploying new plug-ins and standalone applications with each passing day. The goal of this book is to show you how this toolset functions and how you can use these tools for your own applications.
In particular, youâll be able to
- Develop SWT/JFace-based applications with hands-on code examples
- Create customized graphics with SWTâs built-in graphical context
- Understand the structure and methodology behind the SWT/JFace API
- Further your knowledge of GUI (graphical user interface) design
- Build and deploy SWT/JFace applications for Eclipse and standalone usage
Most important, GUI development should be fun! No other branch of programming provides the same satisfaction as watching a new graphical interface spring to life. Therefore, weâll intersperse the theory of SWT and JFace with example code showing practical GUI development.
But before we start programming, we need to show you what this new technology is all about and what tasks it will help you perform.
1.1. What is SWT/JFace?
Although we refer to SWT and JFace as tools or toolsets, theyâre essentially software libraries. They consist of packages that contain Java classes and interfaces. But what makes these components so special is that you can combine them to form GUIs. And not just any GUIs, either! Your applications will run quickly, make effective use of computer memory, and, like chameleons, assume the look and feel of whichever Java-supported operating system they run on. No other GUI-building library can say that.
Although SWT and JFace accomplish the same goal, they follow different philosophies in creating user interfaces. Our favorite analogy involves automobile transmissions. SWT development is like using a standard transmission: It gives you greater control and access to the system internals, but itâs more complicated to use. JFace, on the other hand, resembles an automatic transmission: It does most of the work for you, but you lose flexibility.
Of course, the truth is more complicated than any analogy. So, letâs investigate these two libraries in greater depth.
1.1.1. Building GUIs with SWT
Every operating system contains a number of graphical components that make up its default user interface. These include buttons, windows, menus, and everything else you see on your computer screen. The goal of SWT is to give you, the Java programmer, direct access to these components so that you can configure and position them however you like.
You donât have to worry about the end userâs operating system. When you add an SWT Button object to your application, it will look and act like a Windows button on Windows, a Macintosh button on Macintosh, and a Linux button on a Linux system. Users will think that you wrote the GUI specifically for their machines, and theyâll have no idea that you wrote the code only once using SWT.
In addition to graphical components, SWT also provides access to events. This means you can keep track of what buttons your users have clicked and which menu items theyâve selected. This powerful capability makes it possible to receive and respond to nearly every form of user input, and weâll spend a great deal of time showing how this works.
Finally, if you want to add graphics to your application, SWT provides a large set of tools for creating images, working with new fonts, and drawing shapes. This feature not only allows you to build new graphics, but also lets you control how, when, and where theyâre displayed in your GUI. This book will show you how SWT manages colors, drawings, fonts, and images, and will present a great deal of example code.
SWT provides a wealth of capabilities for building user interfaces, but as youâll see in this book, the code can become lengthy and complex. For this reason, the Eclipse designers built a second library for GUI development: JFace.
1.1.2. Simplifying GUI development with JFace
Rather than write the same SWT code over and over again, the designers of the Eclipse Workbench created JFace. This library provides shortcuts around many of the tasks that can be time-consuming using SWT alone. But JFace is not a replacement for SWT, and many GUIs will need features from both toolsets.
An important example of JFaceâs increased efficiency involves events. In many user interfaces, you may have different events, such as button clicks, keystrokes, or menu selections, that all perform the same function. In SWT, each event needs to be received and handled separately. But JFace allows you to combine them into a single object, so you can concern yourself with the eventâs response instead of the component that triggered it. This simple but powerful concept makes it possible to add context menus, toolbars, and palettes to your GUIs without adding a lot of code.
JFace is also helpful when youâre building large GUIs that require multiple windows and graphics. It provides registry classes that help you organize SWT components and manage their memory allocation. For example, in SWT, you need to specifically create and deallocate every Font and Image in your application. But with JFace, you can use built-in FontRegistry and ImageRegistry objects to take care of these tedious concerns for you.
Now that you understand the basic characteristics behind these two libraries, we need to dig a little deeper and show you the concepts behind their design. This discussion will explain why SWT/JFace GUIs are so fast, why they can take the appearance of whatever operating system they run on, and why they were created in the first place.
1.2. Looking under the hood
Adding components, events, and graphics to a user interface isnât a new idea. Therefore, to see why the SWT/JFace toolset has caused such a stir, you need to understand what its designers were thinking. This means investigating the principles behind Java GUI development and how these libraries make use of them.
But before we can investigate SWT/JFace in depth, we need to introduce Swing. SWT and JFace were created in response to this library, and by understanding the contrast between the two design philosophies, youâll better appreciate how SWT and JFace function. Further, in addition to recognizing the trade-offs between Swing and SWT/JFace, youâll be able to participate in the passionate debates concerning the two.
1.2.1. The old standby: Swing
When Sun released the Swing library in 1998, the Java community was delighted. Finally, Sun had backed up its âWrite Once, Run Anywhereâ credo with a toolset capable of building platform-independent user interfaces. Swing quickly became the most popular tool for creating GUIs in Java.
But as time went by, many developers became discontented. The qualities that made Swing so attractive initially also made for complex development and slow operation. For this reason, Java GUIs have found little use in desktop applications.
Swing rendering
In order to ensure consistent appearance and operation across operating systems, Swing takes complete control of rendering its user interfaces...