|[ Team LiB ]|
As we've just discussed, the first step in creating a GUI is to create and configure the components that comprise it. To do this, you need to be familiar with the components that are available to you and their methods. Chapter 2 of Java Foundation Classes in a Nutshell contains tables that list the available AWT and Swing components. You can also find this information by looking at a listing of the classes in the java.awt and javax.swing packages; it is particularly easy to identify Swing components, since their names all begin with the letter J.
Every component defines a set of properties you can use to configure it. A property is a named attribute of a component whose value you can set. Typical component properties have such names as font, background, and alignment. You set a property value with a setter method and query it with a getter method. Setter and getter methods are collectively called property accessor methods, and their names usually begin with the words "set" and "get". The notion of component properties is formally defined by the JavaBeans specification; we'll see more about them in Chapter 15. For now, however, an informal understanding will suffice: components can be configured by invoking various methods whose names begin with "set". Remember that components inherit many methods from their superclasses, notably java.awt.Component and javax.swing.JComponent, so just because a component class does not define a setter method directly does not mean that the component does not support a particular property.
Swing includes a large number of components, and it is not feasible to include examples of each one in this chapter. Later in the chapter, we'll see examples that highlight particularly powerful components for displaying tables, trees, and formatted text, and there are examples of more commonplace components—such as buttons, checkboxes, and menus—throughout the chapter. Instead of providing a guided tour to the complete set of Swing components, I instead invite you to conduct your own, using the ShowBean program developed later in this chapter (and also in Chapter 15).
As its name implies, ShowBean is a program for viewing "JavaBeans components." All Swing and AWT components are beans, so you can use it to view and experiment with these components and their properties. Invoke ShowBean with the name of one or more component classes on the command line. Each class name may be followed by zero or more property specifications of the form name=value. The program creates an instance of each named class and configures it by setting the specified properties to the given values. The program then displays the components using a Swing JTabbedPane container within a JFrame window. For example, to create the window displayed in Figure 11-1, I invoked ShowBean as follows (this is one long command line that has been wrapped onto multiple lines):
% java je3.gui.ShowBean javax.swing.JButton 'text=Hello World!' font=helvetica-bold-48 javax.swing.JRadioButton 'text=pick me' java.awt.Button label=Hello javax.swing.JSlider
In addition to the ability to set property values from the command line, the Properties menu allows you to view and set the values of certain properties. Similarly, the Commands menu allows you to invoke certain methods on the component. This is useful for experimenting with no-argument methods, such as the cut( ) and paste( ) methods inherited by JTextField. The Properties menu is most useful if ShowBean has access to "bean info" for the Swing components viewed with the program; in this case, it can display tooltips that give a short description of each property in the menu. BeanInfo classes for Swing components are contained in the file lib/dt.jar in Sun's Java SDK. Put this file in your classpath before running ShowBean, or copy it to the jre/lib/ext/ directory to make it permanently available to all programs. "dt" stands for "design time"—the files in dt.jar provide design-time enhancements for GUI builders and programs like ShowBean, but are not required by any of the components at runtime.
One of the powerful features of the Swing component library is its use of a pluggable look-and-feel architecture, which enables an entire GUI to change its appearance. The default appearance, known as the "Metal" look-and-feel, is the same across all platforms. However, a program can choose any installed look-and-feel, including ones that simulate the appearance of native OS applications. One of the interesting features of the ShowBean program is its pull-down menu labeled Look and Feel. It allows you to choose from any of the installed look-and-feels.
Many examples in this chapter (as well as some examples in subsequent chapters) take the form of component subclasses rather than complete programs with a main( ) method. In addition to using the ShowBean program as a way to experiment with predefined Swing and AWT components, we'll also use it as a viewer for other examples.
|[ Team LiB ]|