Page 1 of 1

Event-Driven Programming

#1 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Posted 27 June 2010 - 06:52 PM

*
POPULAR

Event-driven programming is very different from "traditional" programming, and probably most prevalent when working with Graphical User Interfaces and Game Programming. In more "traditional," we perform tasks like prompting the user for input, evaluating that input and responding to that input in a loop, with significant control being placed on the users' actions. So for example:
int[] x = new int[10];
Scanner scan = new Scanner(System.in);
for(int i = 0; i < x.length; i++){
   System.out.println("Enter number " + (i+1)); 
   x[i] = scan.nextInt();
}



Event-driven programming is different, though. Rather than placing so much control on gathering input, we focus on responding to that input. So basically, when a button is clicked, we write the code to respond. When a key is pressed, or the mouse moved, etc., we respond to those respective events. This is how GUI programming is, as well as a lot of Game Programming. So if the user doesn't select something, then our program never responds. This may not be extremely clear right now, but after we examine the components of an Event-Driven model, I think the differences will become more apparent.

In any Event-driven model, we have three components: the trigger, the listener, and the response. The trigger is usually something the users interact with (like a JButton), but not always (in the case of Swing Timer). Basically, it fires an event. This is where the Listener comes in. As a trigger cannot listen for its own events, we need a separate component to do such. The Listener basically invokes the response.

An analogy for the event-driven model would be a light switch. We have the trigger (the switch), the Listener (the wiring), and the response (the light turning on). In addition, it is there for the user to click at will, but we are not notifying and prompting them to turn on the light.

A basic example illustrating the event-driven model in Java, commented to identify the trigger, listener, and response.
//we have a class which will display a window
//and listen for the button clicks
public class MyFrame extends JFrame implements ActionListener{
 
   private JButton button = new JButton();
   private int count = 0;
  
   public MyFrame(){
       this.setSize(200,200);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(button);

        //we are telling button that it has a listener
        //for its ActionEvent, marking the JButton as the trigger
        //and this object as the listener
        button.addActionListener(this);
        this.add(button);
        this.setVisible(true);
   }

   //this is our response
   //when the listener is notified of an event
   //being fired, it will invoke this method
   public void actionPerformed(ActionEvent e){
       button.setText("I have been clicked " + (++count) + " times");
   }

}



Is This A Good Question/Topic? 8
  • +

Replies To: Event-Driven Programming

#2 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Posted 27 June 2010 - 10:52 PM

Good ole' intro to Java stuff. Nice example!
Was This Post Helpful? 0
  • +
  • -

#3 valeria7386  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-July 10

Posted 01 July 2010 - 06:39 AM

it is very useful post for the java programmers
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Posted 01 July 2010 - 03:14 PM

This is definitely helpful with Java, but also very applicable to other languages and platforms with GUI and game programming, like ActionScript, C++, HTML and Javascript which seems to be your business :), etc.
Was This Post Helpful? 0
  • +
  • -

#5 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Posted 27 August 2010 - 07:32 PM

Interesting tutorial, I hit the + button for ya!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1