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

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. See Liang Table 10.1 (page 418) 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.addListener(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 import javax.swing.*; import java.awt.*; import java.awt.event.*; // we need event classes class myCountListener implements ActionListener{ // listener class public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,”Count button clicked!"); } class myButtonJFrame extends JFrame{ myButtonJFrame (){ // constructor super(“myButtonJFrame"); JButton myCountButton = new JButton("Count"); // make button myCountListener listener = new myCountListener(); // make listener myCountButton.addActionListener(listener); // register listener // with myCountButton JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4,0)); buttonPanel.add(myCountButton); // add button to panel Container content = this.getContentPane(); content.setLayout(new BorderLayout()); content.add(new JTextArea(6,30),BorderLayout.CENTER); content.add(buttonPanel, BorderLayout.EAST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } Download from herehere

8 class testMyButtonJFrame { public static void main(String[] args) { myButtonJFrame frame = new myButtonJFrame(); frame.setSize(300,200); frame.setLocation(300,300); frame.setVisible(true); } This piece of code contains three different classes: The countListener class, which implements ActionListener (objects from this class will listen for the count button to be clicked) The myButtonJFrame class (which will contain a count button, and a JTextArea). The testMyButtonFrame class, which has a main method which creates a myButtonJFrame object for us to try it out. When this code is run, whenever the “count” button is clicked, we get a JOptionPane popping up to say “count button clicked!”.

9 Making the JFrame class implement ActionListener We’ve seen a very simple class that implements 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

10 import javax.swing.*; import java.awt.*; import java.awt.event.*; // we need event classes class EventButtonJFrame extends Jframe implements ActionListener{ EventButtonJFrame (){ // constructor super(“myButtonJFrame"); JButton myCountButton = new JButton("Count"); // make button myCountButton.addActionListener(this); // register this class as the listener // for myCountButton JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4,0)); buttonPanel.add(myCountButton); // add button to panel Container content = this.getContentPane(); content.setLayout(new BorderLayout()); content.add(new JTextArea(6,30),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

11 Events, Events, Events Other important events for which listeners may be provided include: –mouse events –key events –focus events –text events......etc Download an example MouseListener from herehere

12 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”);

13 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); // the myTextArea variable: we will getText() from this EventButtonJFrame (){ super("myButtonJFrame"); JButton myCountButton = new JButton("count"); // create button myCountButton.addActionListener(this); // add listener JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4,0)); buttonPanel.add(myCountButton); buttonPanel.add(new JButton("clear")); // add clear 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

14 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…

15 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 }

16 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.4.2).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