710 Graphical User Interface Components: Part 1 Chapter (Web domain)
Friday, February 29th, 2008710 Graphical User Interface Components: Part 1 Chapter 12 The original GUI components from the Abstract Windowing Toolkit package java.awt are tied directly to the local platform s graphical user interface capabilities. Swing components are lightweight components. AWT components are tied to the local platform and are called heavyweight components they must rely on the local platform s windowing system to determine their functionality and their look and feel. Several Swing GUI components are heavyweight GUI components: in particular, subclasses of java.awt.Window (such as JFrame) that display windows on the screen. Heavyweight Swing GUI components are less flexible than lightweight components. Much of each Swing GUI component s functionality is inherited from classes Component, Containerand JComponent(the superclass to most Swing components). A Container is an area where components can be placed. JLabels provide text instructions or information on a GUI. JComponent method setToolTipText specifies the tool tip that is displayed automatically when the user positions the mouse cursor over a JComponent in the GUI. Many Swing components can display images by specifying an Icon as an argument to their constructor or by using a method setIcon. Class ImageIcon (package javax.swing) supports several image formats, including Portable Network Graphics (PNG), Graphics Interchange Format (GIF) and Joint Photographic Experts Group (JPEG). Interface SwingConstants (package javax.swing) defines a set of common integer constants (such as SwingConstants.LEFT) that are used with many Swing components. By default, the text of a JComponent appears to the right of the image when the JComponent contains both text and an image. The horizontal and vertical alignments of a JLabel can be set with methods setHorizontal- Alignmentand setVerticalAlignment. Method setTextsets the text displayed on the label. Method getText retrieves the current text displayed on a label. Methods setHorizontalTextPositionand setVerticalTextPositionspecify the text position in a label. JComponent method setIcon sets the Icon displayed on a JComponent. Method get- Icon retrieves the current Icon displayed on a JComponent. GUIs generate events when the user interacts with the GUI. Information about a GUI event is stored in an object of a class that extends AWTEvent. To process an event, the programmer must register an event listener and implement one or more event handlers. The use of event listeners in event handling is known as the delegation event model the processing of an event is delegated to a particular object in the program. When an event occurs, the GUI component with which the user interacted notifies its registered listeners by calling each listener s appropriate event handling method. JTextFieldsand JPasswordFields are single-line areas in which text can be entered by the user from the keyboard or text can simply be displayed. A JPasswordField shows that a character was typed as the user enters characters, but automatically hides the characters. When the user types data into a JTextFieldor JPasswordField and presses the Enter key, an ActionEvent occurs. JTextComponent method setEditabledetermines whether the user can modify the text in a JTextComponent. JPasswordField method getPasswordreturns the password as an array of type char. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01