Archive for January, 2008

Chapter 12 Graphical User Interface Components: Part 1 (Dedicated web hosting)

Friday, January 25th, 2008

Chapter 12 Graphical User Interface Components: Part 1 659 Fig. 12.7 Demonstrating JTextFields and JPasswordFields (part 4 of 4). Lines 12 13 declare three references for JTextFields (textField1, textField2 and textField3) and a JPasswordField (passwordField). Each of these is instantiated in the constructor (line 16 50). Line 24 defines JTextField textField1 with 10 columns of text. The width of the text field will be the width in pixels of the average character in the text field s current font multiplied by 10. Line 25 adds textField1to the content pane. Line 28 defines textField2with the initial text “Entertexthere”to display in the text field. The width of the text field is determined by the text. Line 29 adds textField2to the content pane. Line 33 defines textField3and call the JTextFieldconstructor with two arguments the default text “Uneditabletextfield”to display in the text field and the number of columns (20). The width of the text field is determined by the number of columns specified. Line 34 uses method setEditable(inherited into JTextFieldfrom class JTextComponent) to indicate that the user cannot modify the text in the text field. Line 35 adds textField3to the content pane. Line 38 defines JPasswordField passwordField with the text “Hidden text” to display in the text field. The width of the text field is determined by the text. Notice that the text is displayed as a string of asterisks when the program executes. Line 39 adds passwordFieldto the content pane. For the event-handling in this example, we defined inner class TextFieldHandler (lines 62 92), which implements interface ActionListener(class TextFieldHandler is discussed shortly). Thus, every instance of class TextFieldHandler is an ActionListener. Line 42 defines an instance of class TextFieldHandler and assigns it to reference handler. This one instance will be used as the event-listener object for the JTextFields and the JPasswordFieldin this example. Lines 43 46 are the event registration statements that specify the event listener object for each of the three JTextFields and for the JPasswordField. After these statements execute, the object to which handler refers is listening for events (i.e., it will be notified when an event occurs) on these four objects. The program calls JTextField method addActionListenerto register the event for each component. Method addAction- Listener receives as its argument an ActionListener object. Thus, any object of a class that implements interface ActionListener (i.e., any object that is an Action Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

658 Graphical User Interface Components: Part 1 Chapter (Web design course)

Thursday, January 24th, 2008

658 Graphical User Interface Components: Part 1 Chapter 12 60 61 // private inner class for event handling 62 private class TextFieldHandler implements ActionListener { 63 64 // process text field events 65 public void actionPerformed( ActionEvent event ) 66 { 67 String string = “”; 68 69 // user pressed Enter in JTextField textField1 70 if ( event.getSource() == textField1 ) 71 string = “textField1: ” + event.getActionCommand(); 72 73 // user pressed Enter in JTextField textField2 74 else if ( event.getSource() == textField2 ) 75 string = “textField2: ” + event.getActionCommand(); 76 77 // user pressed Enter in JTextField textField3 78 else if ( event.getSource() == textField3 ) 79 string = “textField3: ” + event.getActionCommand(); 80 81 // user pressed Enter in JTextField passwordField 82 else if ( event.getSource() == passwordField ) { 83 JPasswordField pwd = 84 ( JPasswordField ) event.getSource(); 85 string = “passwordField: ” + 86 new String( passwordField.getPassword() ); 87 } 88 89 JOptionPane.showMessageDialog( null, string ); 90 } 91 92 } // end private inner class TextFieldHandler 93 94 } // end class TextFieldTest Fig. 12.7 Demonstrating JTextFields and JPasswordFields (part 3 of 4). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface Components: Part 1 (My web site)

Wednesday, January 23rd, 2008

Chapter 12 Graphical User Interface Components: Part 1 657 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class TextFieldTest extends JFrame { 12 private JTextField textField1, textField2, textField3; 13 private JPasswordField passwordField; 14 15 // set up GUI 16 public TextFieldTest() 17 { 18 super( “Testing JTextField and JPasswordField” ); 19 20 Container container = getContentPane(); 21 container.setLayout( new FlowLayout() ); 22 23 // construct textfield with default sizing 24 textField1 = new JTextField( 10 ); 25 container.add( textField1 ); 26 27 // construct textfield with default text 28 textField2 = new JTextField( “Enter text here” ); 29 container.add( textField2 ); 30 31 // construct textfield with default text and 32 // 20 visible elements and no event handler 33 textField3 = new JTextField( “Uneditable text field”, 20 ); 34 textField3.setEditable( false ); 35 container.add( textField3 ); 36 37 // construct textfield with default text 38 passwordField = new JPasswordField( “Hidden text” ); 39 container.add( passwordField ); 40 41 // register event handlers 42 TextFieldHandler handler = new TextFieldHandler(); 43 textField1.addActionListener( handler ); 44 textField2.addActionListener( handler ); 45 textField3.addActionListener( handler ); 46 passwordField.addActionListener( handler ); 47 48 setSize( 325, 100 ); 49 setVisible( true ); 50 } 51 52 // execute application 53 public static void main( String args[] ) 54 { 55 TextFieldTest application = new TextFieldTest(); 56 57 application.setDefaultCloseOperation( 58 JFrame.EXIT_ON_CLOSE ); 59 } Fig. 12.7 Demonstrating JTextFields and JPasswordFields (part 2 of 4). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

656 Graphical User Interface Components: Part 1 Chapter (Web host forum)

Tuesday, January 22nd, 2008

656 Graphical User Interface Components: Part 1 Chapter 12 An event listener object listens for specific types of events generated by event sources (normally GUI components) in a program. An event handler is a method that is called in response to a particular type of event. Each event-listener interface specifies one or more event-handling methods that must be defined in the class that implements the event-listener interface. Remember that interfaces define abstractmethods. Any class that implements an interface must define all the methods of that interface; otherwise, the class is an abstractclass and cannot be used to create objects. 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 (the listener) 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. For example, when the user presses the Enter key in a JTextField, the registered listener s actionPerformed method is called. How did the event handler get registered? How does the GUI component know to call actionPerformed as opposed to some other event handling method? We answer these questions and diagram the interaction as part of the next example. 12.5 JTextFieldand JPasswordField JTextFields and JPasswordFields (package javax.swing) are single-line areas in which text can be entered by the user from the keyboard or text can simply be displayed. A JPasswordFieldshows that a character was typed as the user enters characters, but hides the characters, assuming that they represent a password that should remain known only to the user. When the user types data into a JTextField or JPasswordField and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the JTextFieldor JPassword- Fieldat the time of the event in the program. Class JTextFieldextends class JTextComponent(package javax.swing.text), which provides many features common to Swing s text-based components. Class JPasswordFieldextends JTextFieldand adds several methods that are specific to processing passwords. Common Programming Error 12.3 Using a lowercase fin the class names JTextFieldor JPasswordFieldis a syntax error. The application of Fig. 12.7 uses classes JTextField and JPasswordField to create and manipulate four fields. When the user presses Enter in the currently active field (the currently active component has the focus ), a message dialog box containing the text in the field is displayed. When an event occurs in the JPasswordField, the password is revealed. 1 // Fig. 12.7: TextFieldTest.java 2 // Demonstrating the JTextField class. 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; Fig. 12.7 Demonstrating JTextFields and JPasswordFields (part 1 of 4). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 (Web server application) Graphical User Interface Components: Part 1

Tuesday, January 22nd, 2008

Chapter 12 Graphical User Interface Components: Part 1 655 There are three parts to the event-handling mechanism the event source, the event object and the event listener. The event source is the particular GUI component with which the user interacts. The event object encapsulates information about the event that occurred. This information includes a reference to the event source and any event-specific information that may be required by the event listener to handle the event. The event listener is an object that is notified by the event source when an event occurs. The event listener receives an event object when it is notified of the event, then uses the object to respond to the event. The event source is required to provide methods that enable listeners to be registered and unregistered. The event source also is required to maintain a list of its registered listeners and be able to notify its listeners when an event occurs. The programmer must perform two key tasks to process a graphical user interface event in a program register an event listener for the GUI component that is expected to generate the event, and implement an event handling method (or set of event-handling methods). Commonly, event-handling methods are called event handlers. An event listener for a GUI event is an object of a class that implements one or more of the event-listener interfaces from package java.awt.event and package javax.swing.event. Many of the event-listener types are common to both Swing and AWT components. Such types are defined in package java.awt.event, and many of these are shown in Fig. 12.6. Additional event-listener types that are specific to Swing components are defined in package javax.swing.event. java.util.EventListener ActionListener ComponentListener AdjustmentListener ContainerListener MouseListener TextListener ItemListener FocusListener KeyListener MouseMotionListener WindowListener Class name Key Interface name Fig. 12.6Event-listener interfaces of package java.awt.event. 12. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

654 Graphical User Interface Components: Part (Web page design) 1 Chapter

Monday, January 21st, 2008

654 Graphical User Interface Components: Part 1 Chapter 12 Class JLabelprovides many methods to configure a label after it has been instantiated. Line 37 creates a JLabeland invokes the no-argument (default constructor). Such a label has no text or Icon. Line 38 uses JLabel method setText to set the text displayed on the label. A corresponding method getTextretrieves the current text displayed on a label. Line 39 uses JLabelmethod setIconto set the Icondisplayed on the label. A corresponding method getIconretrieves the current Icondisplayed on a label. Lines 40 41 use JLabelmethods setHorizontalTextPositionand setVertical- TextPositionto specify the text position in the label. In this case, the text will be centered horizontally and will appear at the bottom of the label. Thus, the Icon will appear above the text. Line 42 sets the tool tip text for the label3. Line 43 adds label3to the content pane. 12.4 Event-Handling Model In the preceding section, we did not discuss event handling because there are no specific events for JLabelobjects. GUIs are event driven (i.e., they generate events when the user of the program interacts with the GUI). Some common interactions are moving the mouse, clicking the mouse, clicking a button, typing in a text field, selecting an item from a menu, closing a window, etc. When a user interaction occurs, an event is sent to the program. GUI event information is stored in an object of a class that extends AWTEvent. Figure 12.5 illustrates a hierarchy containing many of the event classes we use from package java.awt.event. Many of these event classes are discussed throughout this chapter and Chapter 13. The event types from package java.awt.eventare used with both AWT and Swing components. Additional event types have also been added that are specific to several types of Swing components. New Swing-component event types are defined in package javax.swing.event. Class name Key java.lang.Object java.awt.AWTEvent ActionEvent ItemEvent AdjustmentEvent ComponentEvent java.util.EventObject ContainerEvent PaintEvent FocusEvent WindowEvent InputEvent KeyEvent MouseEventInterface name Fig. 12.5Some event classes of package java.awt.event. 12. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting providers - Chapter 12 Graphical User Interface Components: Part 1

Sunday, January 20th, 2008

Chapter 12 Graphical User Interface Components: Part 1 653 Good Programming Practice 12.4 Study the methods of class javax.swing.JLabel in the Java 2 SDK on-line documentation to learn the complete capabilities of the class before using it. The program declares three JLabels at line 12. The JLabel objects are instantiated in the LabelTest constructor (line 15 47). Line 24 creates a JLabel object with the text “Label with text”. The label displays this text when the label appears on the screen (i.e., when the window is displayed in this program). Line 25 uses method setToolTipText (inherited into class JLabel from class JComponent) to specify the tool tip (see the second screen capture in Fig. 12.4) that is displayed automatically when the user positions the mouse cursor over the label in the GUI. When you execute this program, try positioning the mouse over each label to see its tool tip. Line 26 adds label1 to the content pane. Look-and-Feel Observation 12.3 Use tool tips (set with JComponent method setToolTipText) to add descriptive text to your GUI components. This text helps the user determine the GUI component s purpose in the user interface. Several Swing components can display images by specifying an Icon as an argument to their constructor or by using a method that is normally called setIcon. An Icon is an object of any class that implements interface Icon (package javax.swing). One such class is ImageIcon (package javax.swing), which supports several image formats, including Graphics Interchange Format (GIF), Portable Network Graphics (PNG) and Joint Photographic Experts Group (JPEG). File names for each of these types typically end with .gif, .pngor .jpg (or .jpeg), respectively. We discuss images in more detail in Chapter 18, Multimedia. Line 30 defines an ImageIcon object. The file bug1.gif contains the image to load and store in the ImageIcon object. This file is assumed to be in the same directory as the program (we will discuss locating the file elsewhere in Chapter 18). The ImageIcon object is assigned to Icon reference bug. Remember, class ImageIcon implements interface Icon, therefore an ImageIconis an Icon. Class JLabelsupports the display of Icons. Lines 31 32 use another JLabel constructor to create a label that displays the text “Labelwithtextandicon” and the Icon to which bug refers and is left justified or left aligned (i.e., the icon and text are at the left side of the label s area on the screen). 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 appears to the right of the image when a label contains both text and an image. The horizontal and vertical alignments of a label can be set with methods setHorizontalAlignment and setVerticalAlignment, respectively. Line 33 specifies the tool tip text for label2. Line 34 adds label2 to the content pane. Common Programming Error 12.1 If you do not explicitly add a GUI component to a container, the GUI component will not be displayed when the container appears on the screen. Common Programming Error 12.2 Adding to a container a component that has not been instantiated throws a NullPointer- Exception. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting india - 652 Graphical User Interface Components: Part 1 Chapter

Sunday, January 20th, 2008

652 Graphical User Interface Components: Part 1 Chapter 12 19 // get content pane and set its layout 20 Container container = getContentPane(); 21 container.setLayout( new FlowLayout() ); 22 23 // JLabel constructor with a string argument 24 label1 = new JLabel( “Label with text” ); 25 label1.setToolTipText( “This is label1″ ); 26 container.add( label1 ); 27 28 // JLabel constructor with string, Icon and 29 // alignment arguments 30 Icon bug = new ImageIcon( “bug1.gif” ); 31 label2 = new JLabel( “Label with text and icon”, 32 bug, SwingConstants.LEFT ); 33 label2.setToolTipText( “This is label2″ ); 34 container.add( label2 ); 35 36 // JLabel constructor no arguments 37 label3 = new JLabel(); 38 label3.setText( “Label with icon and text at bottom” ); 39 label3.setIcon( bug ); 40 label3.setHorizontalTextPosition( SwingConstants.CENTER ); 41 label3.setVerticalTextPosition( SwingConstants.BOTTOM ); 42 label3.setToolTipText( “This is label3″ ); 43 container.add( label3 ); 44 45 setSize( 275, 170 ); 46 setVisible( true ); 47 } 48 49 // execute application 50 public static void main( String args[] ) 51 { 52 LabelTest application = new LabelTest(); 53 54 application.setDefaultCloseOperation( 55 JFrame.EXIT_ON_CLOSE ); 56 } 57 58 } // end class LabelTest Fig. 12.4 Demonstrating class JLabel(part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface Components: Part 1 (Free web host)

Saturday, January 19th, 2008

Chapter 12 Graphical User Interface Components: Part 1 651 Good Programming Practice 12.3 Study the methods of class JComponentin the Java 2 SDK on-line documentation to learn the capabilities common to every container for GUI components. Swing components that subclass JComponenthave many features, including: 1. A pluggable look and feel that can be used to customize the look and feel when the program executes on different platforms. 2. Shortcut keys (called mnemonics) for direct access to GUI components through the keyboard. 3. Common event-handling capabilities for cases where several GUI components initiate the same actions in a program. 4. Brief descriptions of a GUI component s purpose (called tool tips) that are displayed when the mouse cursor is positioned over the component for a short time. 5. Support for assistive technologies such as braille screen readers for blind people. 6. Support for user interface localization customizing the user interface for display in different languages and cultural conventions. These are just some of the many features of the Swing components. We discuss several of these features here and in Chapter 13. 12.3 JLabel Labels provide text instructions or information on a GUI. Labels are defined with class JLabel a subclass of JComponent. A label displays a single line of read-only text, an image or both text and an image. Programs rarely change a label s contents after creating it. The application of Figure 12.4 demonstrates several JLabelfeatures. 1 // Fig. 12.4: LabelTest.java 2 // Demonstrating the JLabel class. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class LabelTest extends JFrame { 12 private JLabel label1, label2, label3; 13 14 // set up GUI 15 public LabelTest() 16 { 17 super( “Testing JLabel” ); 18 Fig. 12.4 Demonstrating class JLabel(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

650 Graphical User Interface Components: (Free web host) Part 1 Chapter

Saturday, January 19th, 2008

650 Graphical User Interface Components: Part 1 Chapter 12 fully qualified package name and class name. Much of each GUI component s functionality is derived from these classes. A class that inherits from the Component class is a Component. For example, class Container inherits from class Component, and class Component inherits from Object. Thus, a Container is a Component and is an Object, and a Componentis an Object. A class that inherits from class Container is a Container. Thus, a JComponentis a Container. Software Engineering Observation 12.1 To use GUI components effectively, the javax.swing and java.awt inheritance hierarchies must be understood especially class Component, class Container and class JComponent, which define features common to most Swing components. Class Component defines the common attributes and behaviors of all subclasses of Component. With few exceptions, most GUI components extend class Component directly or indirectly. One method that originates in class Componentthat has been used frequently to this point is paint. Other methods discussed previously that originated in Component are repaintand update. It is important to understand the methods of class Component because much of the functionality inherited by every subclass of Component is defined by the Component class originally. Operations common to most GUI components (both Swing and AWT) are found in class Component. Good Programming Practice 12.1 Study the methods of class Componentin the Java 2 SDK on-line documentation to learn the capabilities common to most GUI components. A Container is a collection of related components. In applications with JFrames and in applets, we attach components to the content pane, which is an object of class Container. Class Container defines the common attributes and behaviors for all subclasses of Container. One method that originates in class Container is add for adding components to a Container. Another method that originates in class Container is setLayout, which enables a program to specify the layout manager that helps a Containerposition and size its components. Good Programming Practice 12.2 Study the methods of class Containerin the Java 2 SDK on-line documentation to learn the capabilities common to every container for GUI components. Class JComponentis the superclass to most Swing components. This class defines the common attributes and behaviors of all subclasses of JComponent. java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent Fig. 12.3Common superclasses of many of the Swing components. 12. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01