Archive for January, 2008

Chapter 12 Graphical User Interface Components: Part 1 (Web design online)

Thursday, January 31st, 2008

Chapter 12 Graphical User Interface Components: Part 1 669 38 39 italicButton = new JRadioButton( “Italic”, false ); 40 container.add( italicButton ); 41 42 boldItalicButton = new JRadioButton( 43 “Bold/Italic”, false ); 44 container.add( boldItalicButton ); 45 46 // register events for JRadioButtons 47 RadioButtonHandler handler = new RadioButtonHandler(); 48 plainButton.addItemListener( handler ); 49 boldButton.addItemListener( handler ); 50 italicButton.addItemListener( handler ); 51 boldItalicButton.addItemListener( handler ); 52 53 // create logical relationship between JRadioButtons 54 radioGroup = new ButtonGroup(); 55 radioGroup.add( plainButton ); 56 radioGroup.add( boldButton ); 57 radioGroup.add( italicButton ); 58 radioGroup.add( boldItalicButton ); 59 60 // create font objects 61 plainFont = new Font( “Serif”, Font.PLAIN, 14 ); 62 boldFont = new Font( “Serif”, Font.BOLD, 14 ); 63 italicFont = new Font( “Serif”, Font.ITALIC, 14 ); 64 boldItalicFont = 65 new Font( “Serif”, Font.BOLD + Font.ITALIC, 14 ); 66 field.setFont( plainFont ); 67 68 setSize( 300, 100 ); 69 setVisible( true ); 70 } 71 72 // execute application 73 public static void main( String args[] ) 74 { 75 RadioButtonTest application = new RadioButtonTest(); 76 77 application.setDefaultCloseOperation( 78 JFrame.EXIT_ON_CLOSE ); 79 } 80 81 // private inner class to handle radio button events 82 private class RadioButtonHandler implements ItemListener { 83 84 // handle radio button events 85 public void itemStateChanged( ItemEvent event ) 86 { 87 // user clicked plainButton 88 if ( event.getSource() == plainButton ) 89 field.setFont( plainFont ); 90 Fig. 12.12 Fig. 12.12 Creating and manipulating radio button (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

668 Graphical User Interface Components: Part 1 Chapter (Web hosting)

Thursday, January 31st, 2008

668 Graphical User Interface Components: Part 1 Chapter 12 options (i.e., multiple options in the group would not be selected at the same time). The logical relationship between radio buttons is maintained by a ButtonGroupobject (package javax.swing). The ButtonGroupobject itself is not a GUI component. Therefore, a ButtonGroup object is not displayed in a user interface. Rather, the individual JRadioButtonobjects from the group are displayed in the GUI. Common Programming Error 12.5 Adding a ButtonGroup object (or an object of any other class that does not derive from Component) to a container is a syntax error. The application of Fig. 12.12 is similar to the preceding program. The user can alter the font style of a JTextField s text. The program uses radio buttons that permit only a single font style in the group to be selected at a time. 1 // Fig. 12.12: RadioButtonTest.java 2 // Creating radio buttons using ButtonGroup and JRadioButton. 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 RadioButtonTest extends JFrame { 12 private JTextField field; 13 private Font plainFont, boldFont, italicFont, boldItalicFont; 14 private JRadioButton plainButton, boldButton, italicButton, 15 boldItalicButton; 16 private ButtonGroup radioGroup; 17 18 // create GUI and fonts 19 public RadioButtonTest() 20 { 21 super( “RadioButton Test” ); 22 23 // get content pane and set its layout 24 Container container = getContentPane(); 25 container.setLayout( new FlowLayout() ); 26 27 // set up JTextField 28 field = 29 new JTextField( “Watch the font style change”, 25 ); 30 container.add( field ); 31 32 // create radio buttons 33 plainButton = new JRadioButton( “Plain”, true ); 34 container.add( plainButton ); 35 36 boldButton = new JRadioButton( “Bold”, false); 37 container.add( boldButton ); Fig. 12.12 Fig. 12.12 Creating and manipulating radio button (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

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

Wednesday, January 30th, 2008

Chapter 12 Graphical User Interface Components: Part 1 667 79 // set text field font 80 field.setFont( 81 new Font( “Serif”, valBold + valItalic, 14 ) ); 82 } 83 84 } // end private inner class CheckBoxHandler 85 86 } // end class CheckBoxTest Fig. 12.11 Program that creates two JCheckBoxbuttons (part 3 of 3). After the JTextFieldis created and initialized, line 27 sets the font of the JText- Field to Serif, PLAIN style and 14-point size. Next, the constructor creates two JCheckBox objects with lines 31 and 34. The String passed to the constructor is the check box label that appears to the right of the JCheckBoxby default. When the user clicks a JCheckBox, an ItemEventoccurs that can be handled by an ItemListener (any object of a class that implements interface ItemListener). An ItemListener must define method itemStateChanged. In this example, the event handling is performed by an instance of inner class CheckBoxHandler(lines 56 84). Lines 38 40 create an instance of class CheckBoxHandler and register it with method addItemListener as the ItemListener for both the bold and italic JCheckBoxes. Method itemStateChanged(lines 61 82) is called when the user clicks either the boldor the italiccheckbox. The method uses event.getSource()to determine which JCheckBox was clicked. If it was JCheckBoxbold, the if/elsestructure at lines 66 69 uses ItemEvent method getStateChange to determine the state of the button (ItemEvent.SELECTED or ItemEvent.DESELECTED). If the state is selected, integer valBold is assigned Font.BOLD; otherwise, valBold is assigned Font.PLAIN. A similar if/else structure is executed if JCheckBox italic is clicked. If the italicstate is selected, integer valItalicis assigned Font.ITALIC; otherwise, valItalic is assigned Font.PLAIN. The sum of valBold and valItalicis used at lines 80 81 as the style of the new font for the JTextField. Radio buttons (defined with class JRadioButton) are similar to check boxes in that they have two states selected and not selected (also called deselected). However, radio buttons normally appear as a group in which only one radio button can be selected at a time. Selecting a different radio button in the group automatically forces all other radio buttons in the group to be deselected. Radio buttons are used to represent a set of mutually exclusive Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

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

Tuesday, January 29th, 2008

666 Graphical User Interface Components: Part 1 Chapter 12 27 field.setFont( new Font( “Serif”, Font.PLAIN, 14 ) ); 28 container.add( field ); 29 30 // create checkbox objects 31 bold = new JCheckBox( “Bold” ); 32 container.add( bold ); 33 34 italic = new JCheckBox( “Italic” ); 35 container.add( italic ); 36 37 // register listeners for JCheckBoxes 38 CheckBoxHandler handler = new CheckBoxHandler(); 39 bold.addItemListener( handler ); 40 italic.addItemListener( handler ); 41 42 setSize( 275, 100 ); 43 setVisible( true ); 44 } 45 46 // execute application 47 public static void main( String args[] ) 48 { 49 CheckBoxTest application = new CheckBoxTest(); 50 51 application.setDefaultCloseOperation( 52 JFrame.EXIT_ON_CLOSE ); 53 } 54 55 // private inner class for ItemListener event handling 56 private class CheckBoxHandler implements ItemListener { 57 private int valBold = Font.PLAIN; 58 private int valItalic = Font.PLAIN; 59 60 // respond to checkbox events 61 public void itemStateChanged( ItemEvent event ) 62 { 63 // process bold checkbox events 64 if ( event.getSource() == bold ) 65 66 if ( event.getStateChange() == ItemEvent.SELECTED ) 67 valBold = Font.BOLD; 68 else 69 valBold = Font.PLAIN; 70 71 // process italic checkbox events 72 if ( event.getSource() == italic ) 73 74 if ( event.getStateChange() == ItemEvent.SELECTED ) 75 valItalic = Font.ITALIC; 76 else 77 valItalic = Font.PLAIN; 78 Fig. 12.11 Program that creates two JCheckBoxbuttons (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web design company - Chapter 12 Graphical User Interface Components: Part 1

Tuesday, January 29th, 2008

Chapter 12 Graphical User Interface Components: Part 1 665 taining the label for the button that was pressed by the user. ActionEvent method getActionCommandreturns the label on the button that generated the event. 12.7 JCheckBoxand JRadioButton The Swing GUI components contain three types of state buttons JToggleButton, JCheckBox and JRadioButton that have on/off or true/false values. JToggle- Buttons are frequently used with toolbars (sets of small buttons typically located on a bar across the top of a window) and are covered in Chapter 13. Classes JCheckBoxand JRadioButton are subclasses of JToggleButton. A JRadioButton is different from a JCheckBox in that there are normally several JRadioButtons that are grouped together and only one of the JRadioButtons in the group can be selected (true) at any time. We first discuss class JCheckBox. Look-and-Feel Observation 12.6 Because class AbstractButtonsupports displaying text and images on a button, all subclasses of AbstractButtonalso support displaying text and images. The application of Fig. 12.11 uses two JCheckBox objects to change the font style of the text displayed in a JTextField. One JCheckBox applies a bold style when selected and the other applies an italic style when selected. If both are selected, the style of the font is bold and italic. When the program initially executes, neither JCheckBox is checked (true). 1 // Fig. 12.11: CheckBoxTest.java 2 // Creating Checkbox buttons. 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 CheckBoxTest extends JFrame { 12 private JTextField field; 13 private JCheckBox bold, italic; 14 15 // set up GUI 16 public CheckBoxTest() 17 { 18 super( “JCheckBox Test” ); 19 20 // get content pane and set its layout 21 Container container = getContentPane(); 22 container.setLayout( new FlowLayout() ); 23 24 // set up JTextField and set its font 25 field = 26 new JTextField( “Watch the font style change”, 20 ); Fig. 12.11 Program that creates two JCheckBoxbuttons (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

664 Graphical User Interface Components: Part 1 Chapter (Apache web server)

Monday, January 28th, 2008

664 Graphical User Interface Components: Part 1 Chapter 12 Fig. 12.10 Fig. 12.10 Demonstrating command buttons and action events (part 3 of 3). Line 12 declares two references to instances of class JButton plainButtonand fancyButton that are instantiated in the constructor. Line 24 creates plainButton with the button label “Plain Button”. Line 25 adds the button to the content pane. A JButtoncan display Icons. To provide the user with an extra level of visual inter- activity with the GUI, a JButton can also have a rollover Icon an Icon that is displayed when the mouse is positioned over the button. The icon on the button changes as the mouse moves in and out of the button s area on the screen. Lines 27 28 create two Image- Iconobjects that represent the default Iconand rollover Iconfor the JButtoncreated at line 29. Both statements assume the image files are stored in the same directory as the program (this is commonly the case for applications that use images). Line 29 creates fancyButton with default text “FancyButton”and the Icon bug1. By default, the text is displayed to the right of the icon. Line 30 uses method set- RolloverIcon (inherited from class AbstractButton into class JButton) to specify the image displayed on the button when the user positions the mouse over the button. Line 31 adds the button to the content pane. Look-and-Feel Observation 12.5 Using rollover icons for JButtons provides the user with visual feedback indicating that if they click the mouse, the button s action will occur. JButtons (like JTextFields) generate ActionEvents. As mentioned previously, an ActionEventcan be processed by any ActionListenerobject. Lines 35 37 register an ActionListener object for each JButton. Inner class ButtonHandler (lines 53 62) defines actionPerformed to display a message dialog box con Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface Components: Part 1 (Web site traffic)

Sunday, January 27th, 2008

Chapter 12 Graphical User Interface Components: Part 1 663 13 14 // set up GUI 15 public ButtonTest() 16 { 17 super( “Testing Buttons” ); 18 19 // get content pane and set its layout 20 Container container = getContentPane(); 21 container.setLayout( new FlowLayout() ); 22 23 // create buttons 24 plainButton = new JButton( “Plain Button” ); 25 container.add( plainButton ); 26 27 Icon bug1 = new ImageIcon( “bug1.gif” ); 28 Icon bug2 = new ImageIcon( “bug2.gif” ); 29 fancyButton = new JButton( “Fancy Button”, bug1 ); 30 fancyButton.setRolloverIcon( bug2 ); 31 container.add( fancyButton ); 32 33 // create an instance of inner class ButtonHandler 34 // to use for button event handling 35 ButtonHandler handler = new ButtonHandler(); 36 fancyButton.addActionListener( handler ); 37 plainButton.addActionListener( handler ); 38 39 setSize( 275, 100 ); 40 setVisible( true ); 41 } 42 43 // execute application 44 public static void main( String args[] ) 45 { 46 ButtonTest application = new ButtonTest(); 47 48 application.setDefaultCloseOperation( 49 JFrame.EXIT_ON_CLOSE ); 50 } 51 52 // inner class for button event handling 53 private class ButtonHandler implements ActionListener { 54 55 // handle button event 56 public void actionPerformed( ActionEvent event ) 57 { 58 JOptionPane.showMessageDialog( null, 59 “You pressed: ” + event.getActionCommand() ); 60 } 61 62 } // end private inner class ButtonHandler 63 64 } // end class ButtonTest Fig. 12.10 Fig. 12.10 Demonstrating command buttons and action events (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web site developers - 662 Graphical User Interface Components: Part 1 Chapter

Sunday, January 27th, 2008

662 Graphical User Interface Components: Part 1 Chapter 12 12.6 JButton A button is a component the user clicks to trigger a specific action. A Java program can use several types of buttons, including command buttons, check boxes, toggle buttons and radio buttons. Figure 12.9 shows the inheritance hierarchy of the Swing buttons we cover in this chapter. As you can see in the diagram, all the button types are subclasses of Abstract- Button(package javax.swing), which defines many of the features that are common to Swing buttons. In this section, we concentrate on buttons that are typically used to initiate a command. Other button types are covered in the next several sections. A command button generates an ActionEventwhen the user clicks the button with the mouse. Command buttons are created with class JButton, which inherits from class AbstractButton. The text on the face of a JButtonis called a button label. A GUI can have many JButtons, but each button label typically should be unique. Look-and-Feel Observation 12.4 Having more than one JButtonwith the same label makes the JButtons ambiguous to the user. Be sure to provide a unique label for each button. The application of Fig. 12.10 creates two JButtons and demonstrates that JButtons (like JLabels) support the display of Icons. Event handling for the buttons is performed by a single instance of inner class ButtonHandler(defined at lines 53 62). javax.swing.JComponent javax.swing.AbstractButton javax.swing.JButton javax.swing.ToggleButton javax.swing.JCheckBox javax.swing.JRadioButton Fig. 12.9The button hierarchy. 12. 1 // Fig. 12.10: ButtonTest.java 2 // Creating JButtons. 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 ButtonTest extends JFrame { 12 private JButton plainButton, fancyButton; Fig. 12.10 Fig. 12.10 Demonstrating command buttons and action events (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

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

Saturday, January 26th, 2008

Chapter 12 Graphical User Interface Components: Part 1 661 textField1 handler This is the JTextField object. It contains an instance variable of type EventListenerList called listenerListthat it inherited from class JComponent. listenerList … This reference is created by the statement This is the TextFieldHandler object that implements ActionListenerand defines method actionPerformed. public void actionPerformed( ActionEvent event ) { // event handled here } textField1.addActionListener( handler ); Fig. 12.8Event registration for JTextFieldtextField1. 12. textField1.addActionListener( handler ); executes in Fig. 12.7, a new entry is placed in the listenerList for JTextField textField1, indicating both the reference to the listener object and the type of listener (in this case ActionListener). The type is important in answering the second question how does the GUI component know to call actionPerformedrather than another event handling method? Every JComponentactually supports several different event types, including mouse events, key events and others. When an event occurs, the event is dispatched only to the event listeners of the appropriate type. The dispatching of an event is simply calling the event handling method for each registered listener for that event type. Each event type has a corresponding event-listener interface. For example, Action- Events are handled by ActionListeners, MouseEvents are handled by MouseListeners (and MouseMotionListeners as we will see) and KeyEvents are handled by KeyListeners. When an event is generated by a user interaction with a component, the component is handed a unique event ID specifying the event type that occurred. The GUI component uses the event ID to decide the type of listener to which the event should be dispatched and the method to call. In the case of an ActionEvent, the event is dispatched to every registered ActionListener s actionPerformed method (the only method in interface ActionListener). In the case of a MouseEvent, the event is dispatched to every registered MouseListener (or MouseMotionListener, depending on the event that occurs). The event ID of the MouseEvent determines which of the seven different mouse event handling methods are called. All of this decision logic is handled for you by the GUI components. We discuss other event types and event-listener interfaces as they are needed with each new component we cover. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web design conference - 660 Graphical User Interface Components: Part 1 Chapter

Friday, January 25th, 2008

660 Graphical User Interface Components: Part 1 Chapter 12 Listener) can be supplied as an argument to this method. The object to which handler refers is an ActionListener because its class implements interface Action- Listener. Now, when the user presses Enter in any of these four fields, method action- Performed(line 65 90) in class TextFieldHandleris called to handle the event. Software Engineering Observation 12.2 The event listener for an event must implement the appropriate event-listener interface. Method actionPerformed uses its ActionEvent argument s method get- Source to determine the GUI component with which the user interacted and creates a String to display in a message dialog box. ActionEvent method getActionCommand returns the text in the JTextField that generated the event. If the user interacted with the JPasswordField, lines 83 84 cast the Component reference returned by event.getSource() to a JPasswordField reference so that lines 85 86 can use JPasswordField method getPassword to obtain the password and create the String to display. Method getPassword returns the password as an array of type char that is used as an argument to a String constructor to create a String. Line 89 displays a message box indicating the GUI component reference name and the text the user typed in the field. Note that even an uneditable JTextField can generate an event. Simply click the text field, then press Enter. Also note that the actual text of the password is displayed when you press Enter in the JPasswordField (of course, you would normally not do this!). Common Programming Error 12.4 Forgetting to register an event handler object for a particular GUI component s event type results in no events being handled for that component for that event type. Using a separate class to define an event listener is a common programming practice for separating the GUI interface from the implementation of its event handler. For the remainder of this chapter and Chapter 13, many programs use separate event-listener classes to process GUI events. 12.5.1 How Event Handling Works Let us illustrate how the event-handling mechanism works using textField1 from the preceding example. We have two remaining open questions from Section 12.4: 1. How did the event handler get registered? 2. How does the GUI component know to call actionPerformedas opposed to some other event handling method? The first question is answered by the event registration performed in lines 43 46 of the program. Figure 12.8 diagrams JTextField reference textField1, the JTextField object to which it refers and the listener object that is registered to handle the JText- Field s event. Every JComponent has an object of class EventListenerList (package javax.swing.event) called listenerList as an instance variable. All registered listeners are stored in the listenerList (diagramed as an array in Figure 12.8). When the statement Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01