Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.

Similar presentations


Presentation on theme: "Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate."— Presentation transcript:

1 Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate what happens and when Sample event: a button click...or mouse movement (see Horstmann, ch 12: Event handling)

2 Events An Event is caused by a user’s action (e.g. clicking) The source of the event is the component generating the event (e.g. a JButton ) The Event Type depends on the nature of the event and of the component: –A JButton produces an ActionEvent when clicked –A JTextArea produces an ActionEvent when return is typed –A Window (e.g. a Jframe ) produces a WindowEvent when opened or closed,etc. All events are subclasses of the EventObject class: import java.awt.event.*; // to get EventObject How do we write code to respond to an event? We use code that “listens” for an event to happen.

3 Noticing events: A Button Click A JFrame contains a JButton To respond when the JButton is clicked, we create an actionListener object to listen for the action of that JButton being clicked. That actionListener object must contain a method called public void ActionPerformed(ActionEvent e) The actionListener object registers with the Jbutton. Thereafter, whenever the JButton is clicked, it calls the actionPerformed(ActionEvent e) method in the listener. The actionPerformed (ActionEvent e) method does whatever we want to happen when the button is clicked. count myActionListener { actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(“Button click!”); } event e: Click Listener registers with JButton JOptionPane Button Clicked!

4 ActionEvent and ActionListener A button click is an ActionEvent Therefore anything that listens for a button click must be an ActionListener Any object that is an ActionListener must have a method like this: –void actionPerformed(ActionEvent e) This method contains code which will be executed when the listener receives notice of an event ActionListener is an interface. Any object that is going to listen for actions must implement the ActionListener interface. Implementing the ActionListener interface is simply a promise to have a method: void ActionPerformed(ActionEvent e)

5 Defining an actionListener class class myCountListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,”Count button clicked!"); } This class defines a set of objects that implement ActionListener and provide the actionPerformed method. We’ll use an object from this class to listen for clicks to the “count” button in a JFrame. Anything can implement the ActionListener interface, as long as it provides the public void actionPerformed(ActionEvent e) Method. Later we’ll see a more complicated class that implements this interface.

6 Registering a listener object with a source (a Jbutton) JButton myCountButton = new JButton(“count"); myCountListener listener = new myCountListener(); myCountButton.addActionListener(listener); This snippet of code creates a JButton object, then creates a myCountListener object, the registers that listener with that button (adds the listener object as a listener to the button). This registration step creates the link between the button and the listener: after this step, when the button is clicked, the actionPerformed() method in the listener object will be executed. Next: putting it all together.

7 Making the JFrame class implement ActionListener Any class at all can implement ActionListener, if it provides the actionPerformed() method. Its most useful to make the class that draws our windows (our JFrames ) also implement ActionListener, so that objects from that clss not only draw buttons but can respond to events on those buttons. To do this we change our myButtonJFrame class so that it proves an actionPerformed() method as well as constructing the JFrame object. I’ll call this new class EventButtonJFrame

8 import javax.swing.*; import java.awt.*; import java.awt.event.*; // we need event classes class EventButtonJFrame extends Jframe implements ActionListener{ JTextArea myTextArea = new JTextArea(6,30), JButton myCountButton = new JButton("Count"); // make button JPanel buttonPanel = new JPanel(); EventButtonJFrame (){ // constructor super(“myButtonJFrame"); myCountButton.addActionListener(this); // register this class as the listener // for myCountButton buttonPanel.setLayout(new GridLayout(4,0)); buttonPanel.add(myCountButton); // add button to panel Container content = this.getContentPane(); content.setLayout(new BorderLayout()); content.add(myTextArea,BorderLayout.CENTER); content.add(buttonPanel, BorderLayout.EAST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,”Count button clicked!"); } This class draws our button frame, but also implements ActionListener and provides the actionPerformed() method

9 Doing something useful! The actionPerformed method in the previous programs doesn’t really do anything useful. To get that method to do something useful, we could, for example, give it access to the JTextArea object created in our JFrame window. To do this, we will 1) Make the JTextArea object a generally accessible variable in the Jframe object. To do this, we put JTextArea myTextArea = new JTextArea("this is a text area",6,30); at the top of the EventButtonJFrame class definition. 2) Refer to the text in that JtextArea object in the actionPerformed method, so that when our count button is clicked, we can return some useful information. We can get the text inside the JTextArea object by saying myTextArea.getText(); (we can change that text by saying something like: myTextArea.setText(“this is the new text”);

10 import javax.swing.*; import java.awt.*; import java.awt.event.*; // we need event classes class EventButtonJFrame extends JFrame implements ActionListener{ JTextArea myTextArea = new JTextArea("this is a text area",6,30); JButton myCountButton = new JButton("count"); // create button JPanel buttonPanel = new JPanel(); EventButtonJFrame (){ super("myButtonJFrame"); myCountButton.addActionListener(this); // add listener buttonPanel.setLayout(new GridLayout(4,0)); buttonPanel.add(myCountButton); // add button Container content = this.getContentPane(); content.setLayout(new BorderLayout()); content.add(myTextArea,BorderLayout.CENTER); // add text area content.add(buttonPanel, BorderLayout.EAST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, “The number of characters in JTextArea is "+myTextArea.getText().length() ); } Whenever the count button is clicked, we get a message telling us how many characters are currently in myTextArea. Download from herehere

11 class testEventButtonJFrame { public static void main(String[] args) { EventButtonJFrame frame = new EventButtonJFrame(); frame.setSize(300,200); frame.setLocation(300,330); frame.setVisible(true); } This program will respond to a click on the count button by popping up a text message telling the user how many characters are currently entered in the JTextArea of the EventButtonJFrame object. Note that for this program to compile and run properly, it must be in a file called testEventButtonJFrame.java. This is because the class with the main method (the one that will actually run) is the testEventButtonJFrame class. // continued…

12 A listener with two buttons (download here)here If we want our actionListener to respond to more than 1 button, we have to change the actionPerformed method so that it checks which button was source of the current event. We do this as follows: Declare the two buttons as class-level variables; at top of the class, put: JButton myCountButton = new JButton("count"); JButton myClearButton = new JButton("clear"); In the constructor for the class, register both buttons with the listener (and add each button to the JPanel as required): myCountButton.addActionListener(this); myClearButton.addActionListener(this); In the actionPerformed method, check which button was the source of the current ActionEvent e, and respond accordingly: if (e.getSource().equals(myCountButton)) {… do whatever } if (e.getSource().equals(myClearButton)) {… do whatever }

13 Listening for MouseEvents Action events are clicks on a button on the screen. We can also listen for “mouse events”: movements of the mouse, clicking on the mouse, etc. The listener interface we use is different: –MouseListener Rather than ActionListener (used earlier). Rather than registering this object as an ActionListener, we register it as a mouseListener ( addMouseListener ). We also make sure the object implements the methods required to be a MouseListener (for ActionListener, only one method, actionPerformed(ActionEvent e) was needed. MouseListener requires more methods)

14 What methods must an object have to implement MouseListener? Check the definition for MouseListener in the API: We must implement all these to implement the interface Method Summary void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed andmouseClickedMouseEvent released) on a component. void mouseEntered(MouseEvent e) Invoked when the mouse enters a component. mouseEnteredMouseEvent void mouseExited(MouseEvent e) Invoked when the mouse exits a component. mouseExitedMouseEvent void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component. mousePressedMouseEvent void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component. mouseReleasedMouseEvent (download here)here

15 Conclusion GUI construction and control is not complicated, but there are loads of details involved. You don’t need to know all these details: my aim has been to give a general idea of what goes on in GUI construction and in event-driven programming. If you’re interested, you can look up the various classes discussed here in the Java API: http://java.sun.com/j2se/ (click on ‘API specifications’ under ‘reference’, and then pick the edition of J2SE you are using: currently probably 1.5.0 ).http://java.sun.com/j2se/ There are very useful tutorials on JFrame use, event-driven programming and other aspects of Java programming in the API.


Download ppt "Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate."

Similar presentations


Ads by Google