1 / 27

GUI and Event-Driven Programming

GUI and Event-Driven Programming. Part 2. Event Handling. An action involving a GUI object, such as clicking a button, is called an event. The mechanism to process events is called event handling .

hedda
Télécharger la présentation

GUI and Event-Driven Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI and Event-Driven Programming Part 2

  2. Event Handling • An action involving a GUI object, such as clicking a button, is called an event. • The mechanism to process events is called event handling. • The event-handling model of Java is based on the concept known as the delegation-based event model. • With this model, event handling is implemented by two types of objects: • event source objects • event listener objects.

  3. Event Sources and Listeners • An event source object is a GUI object where an event occurs. An event source generates events. • An event listener object is an object that includes a method that gets executed in response to the generated events. When an event is generated, the system notifies the relevant event listener objects.

  4. Events and Event Listeners • There are many different kinds of events, but the most common one is an action event. • For the generated events to be processed, we must associate, or register, event listeners to the event sources. • If event sources have no registered listeners, the generated events are ignored.

  5. Events and Event Listeners • An object that can be registered as an action listener must be an instance of a class that is declared specifically for the purpose. We call such classes action listener classes. • To associate an action listener to an action event source, we call the event source’s addActionListener method with the action listener as its argument.

  6. Events and Event Listeners • A single listener can be associated to multiple event sources. • Likewise, multiple listeners can be associated to a single event source. • When an event source generates an event, the system checks for matching registered listeners. • If there is no matching listener, the event is ignored. • If there is a matching listener, the system notifies the listener by calling the listener’s corresponding method.

  7. Action Listener Classes • In the case of action events, the method called is actionPerformed. • To ensure the programmer includes the necessary actionPerformed method in the action listener class, the class must be defined in a specific way. import java.awt.event.*; public class ButtonHandler implements ActionListener { ... }

  8. The ActionListener interface • ActionListener is an interface, not a class. • Like a class, an interface is a reference data type, but unlike a class, an interface includes only constants and abstract methods. • An abstract method has only the method header, or prototype; it has no method body. • A class that implements a Java interface must provide the method body to all the abstract methods defined in the interface.

  9. The ActionListener interface • By requiring an object we pass as an argument to the addActionListener method to be an instance of a class that implements the ActionListener interface, the system ensures that this object will include the necessary actionPerformed method. • Implementation of an interface is Java’s mechanism for providing a form of multiple inheritance

  10. Example • Suppose we want to change the title of the frame, depending on which button is clicked • We can do this using the actionPerformed method. The method model is: public void actionPerformed(ActionEvent evt) { String buttonText = get the text of the event source; JFrame frame = the frame that contains this event source; frame.setTitle(“You clicked “ + buttonText); }

  11. Handling Button Events • The first statement may be handled in one of two ways. • The first way is via the getActionCommand method of the action event object evt: String buttonText = evt.getActionCommand(); • The second way is via the getSource method of the action event object evt.: JButton clickedButton = (JButton) evt.getSource(); String buttonText = clickedButton.getText();

  12. Handling Button Events • A frame window contains nested layers of panes. The topmost pane is called the root pane. • To find the frame that contains the event source, we • get the root pane to which the event source belongs, then • get the frame that contains this root pane. JRootPane rootPane = clickedButton.getRootPane(); Frame frame = (JFrame) rootPane.getParent(); • The frame can be the event listener of the GUI objects it contains. cancelButton.addActionListener(this); okButton.addActionListener(this);

  13. Swing Classes for Text Handling • The Swing GUI classes JLabel, JTextField, and JTextArea all deal with text. • A JLabel object displays uneditable text. • A JTextField object allows the user to enter a single line of text. • A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text.

  14. Text Fields and Action Events • An instance of JTextField generates action events when the object is active and the user presses the ENTER key. • The actionPerformed method must determine which of the event sources (for example, a button or a text field) generated the event. • The instanceof operator determines the class to which the event source belongs. The general model is thus: if (event.getSource() instanceof JButton ){ // event source is either cancelButton // or okButton ... } else { // event source must be inputLine ... }

  15. Text Fields and Action Events • The getText method of JTextField may be used to retrieve the text that the user entered. public void actionPerformed(ActionEvent event) { if (event.getSource() instanceof JButton){ JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle(“You clicked ” + buttonText); } else {// the event source is inputLine setTitle(“You entered ‘” + inputLine.getText() + “’”); } }

  16. JLabel Objects • A JLabel object is useful for displaying a label indicating the purpose of another object, such as a JTextField object. prompt = new JLabel(“Please enter your name”); • JLabel objects may also be used to display images. • When a JLabel object is created, we can pass an ImageIcon object instead of a string. • To create the ImageIcon object, we must specify the filename of the image.

  17. JLabels and Images • We declare the data member private JLabel image; • then create it in the constructor as public TextFrame2 () { ... image = new JLabel(new ImageIcon(“cat.gif”); // assumes the .gif is/in the same directory as the program image.setBounds(10, 20, 50, 50); contentPane.add(image); ... }

  18. Example with JLabel, JButton and JTextField objects

  19. TextArea & Related Methods • A TextArea is like a TextField, but can accommodate more text • Methods include: • setBorder(BorderFactory); // creates a border around a JTextArea object • setEditable(boolean); // enables/disables changes to a JTextArea object • The append(String) method allows us to add text to the text area without replacing old content. • Using the setText (String) method of JTextArea will replace old content with new content.

  20. Example frame with JTextArea object

  21. Adding Scrollbars to a JTextArea JScrollPane scrollText = new JScrollPane(textArea); contentPane.add(scrollText);

  22. Menus • The javax.swing package contains three useful menu-related classes: JMenuBar, JMenu, and JMenuItem. • MenuBar is a bar where the menus are placed. • Menu (such as File or Edit) is a single item in the MenuBar. • MenuItem (such as Copy, Cut, or Paste) is an individual menu choice in the Menu.

  23. Menus • When a MenuItem is selected, it generates a menu action event. • We process menu selections by registering an action listener to menu items. • The following example will create the two menus shown and illustrate how to display and process the menu item selections.

  24. Menus • We will follow this sequence of steps: • Create a JMenuBar object and attach it to a frame. • Create a JMenu object. • Create JMenuItem objects and add them to the JMenu object. • Attach the JMenu object to the JMenuBar object.

  25. Menus • We create a fileMenu object as fileMenu = new Menu(“File”); • The argument to the Menu constructor is the name of the menu. • Menu items appear from the top in the order in which they were added to the menu.

  26. Menus • To create and add a menu item New to fileMenu, we execute item = new MenuItem(“New”); item.addActionListener(this); fileMenu.add(item); • And to add a horizontal line as a separator between menu items, we execute fileMenu.addSeparator();

  27. Menus • We then attach the menus to a menu bar. • We create a MenuBar object in the constructor, call the frame’s setMenuBar method to attach it to the frame, and add the two Menu objects to the menu bar. MenuBar menuBar = new MenuBar(); setMenuBar(menuBar); menuBar.add(fileMenu); menuBar.add(editMenu);

More Related