Components

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 ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.