Hosting web - Chapter 12 Graphical User Interface Components: Part 1
Chapter 12 Graphical User Interface Components: Part 1 711 Every JComponent contains an object of class EventListenerList (package javax.swing.event) called listenerListin which all registered listeners are stored. Every JComponent 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. Each event type has a corresponding event-listener interface. When an event is generated by a user interaction with a component, the component is handed a unique event ID specifying the event type. The GUI component uses the event ID to decide the type of listener to which the event should be dispatched and the event handler method to call. A JButton generates an ActionEvent when the user clicks the button with the mouse. An AbstractButton can have a rollover Iconthat is displayed when the mouse is positioned over the button. The icon changes as the mouse moves in and out of the button s area on the screen. AbstractButton method setRolloverIcon specifies the image displayed on a button when the user positions the mouse over the button. The Swing GUI components contain three state button types JToggleButton, JCheckBox and JRadioButton that have on/off or true/false values. Classes JCheckBox and JRadio- Button are subclasses of JToggleButton. When the user clicks a JCheckBox, an ItemEvent is generated that can be handled by an ItemListener. ItemListeners must define method itemStateChanged. Item- Eventmethod getStateChange determines the state of a JToggleButton. JRadioButtons are similar to JCheckBoxes in that they have two states selected and not selected (also called deselected). JRadioButtons normally appear as a group in which only one radio button can be selected at a time. The logical relationship between radio buttons is maintained by a ButtonGroupobject. The JRadioButtonconstructor supplies the label that appears to the right of the JRadioButton by default and the initial state of the JRadioButton. A true second argument indicates that the JRadioButton should appear selected when it is displayed. JRadioButtons generate ItemEvents when they are clicked. ButtonGroupmethod add associates a JRadioButtonwith a ButtonGroup. If more than one selected JRadioButton object is added to the group, the last selected JRadioButton added will be selected when the GUI is displayed. A JComboBox (sometimes called a drop-down list) provides a list of items from which the user can make a selection. JComboBoxes generate ItemEvents. A numeric index keeps track of the ordering of items in a JComboBox. The first item is added at index 0, the next item is added at index 1 and so forth. The first item added to a JComboBox appears as the currently selected item when the JComboBox is displayed. JComboBoxmethod getSelectedIndex returns the index number of the selected item. A JList displays a series of items from which the user may select one or more items. Class JList supports single- and multiple-selection lists. When an item is clicked in a JList, a ListSelectionEvent occurs. JListmethod setVisibleRowCount determines the number of items that are visible in the list. Method setSelectionMode specifies the selection mode for the list. Class JListdoes not automatically provide a scrollbar if there are more items in the list than the number of visible rows. A JScrollPane object is used to provide the automatic scrolling capability for a JList. A SINGLE_INTERVAL_SELECTION list allows selection of a contiguous range of items by clicking the first item, then holding the Shift key while clicking the last item to select in the range. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01