Handling and Raising events


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Handling and Raising events

  1. #1
    Kent Guest

    Handling and Raising events

    I have a Synchronizer class. I want it to raise an event every X seconds if
    conditions Y & Z are True. Here's what I have already:

    public class Synchronizer implements ActionListener
    {
    timer Synchr; //swing timer

    public Synchronizer(int interval)
    {
    timer Synchr = new timer(interval, this);
    }
    /**
    When the Y & Z set to true, the class will allow timer events to pass.
    */
    public void actionPerformed(ActionEvent e)
    {
    if (Y && Z)
    {
    raiseEvent();
    }
    }
    /**
    I want to raise my own events.
    */
    public void raiseEvent()
    {
    // What goes here?
    }
    // other methods here... [including some to call raiseEvent()]
    boolean Y;
    boolean Z;
    }

    TIA
    Kent



  2. #2
    Kent (2) Guest

    Re: Handling and Raising events


    Nice name :-)

    Your class will need to maintain a set of event listeners. These listeners
    should be configurable via methods such as:

    void addActionListener(ActionListener listener);

    ActionListener removeActionListener(ActionListener listener);

    Assume the listeners are in a private ArrayList called actionListeners:

    private void raiseEvent() {
    ActionEvent event = ...; //construct appropriate event here

    for (int i = 0; i < actionListeners.size(); ++i) {
    ((ActionListener) actionListeners.get(i)).actionPerformed(event);
    //invoke each actionPerformed() method on every listener
    }
    }

    Notice I declared raiseEvent() as private. This is because your class should
    be responsible for invoking this not anyone else.

    Hope that helps,
    Kent

    "Kent" <file_delete@hotmail.com> wrote:
    >I have a Synchronizer class. I want it to raise an event every X seconds

    if
    >conditions Y & Z are True. Here's what I have already:
    >
    >public class Synchronizer implements ActionListener
    >{
    > timer Synchr; //swing timer
    >
    > public Synchronizer(int interval)
    > {
    > timer Synchr = new timer(interval, this);
    > }
    >/**
    > When the Y & Z set to true, the class will allow timer events to pass.
    >*/
    > public void actionPerformed(ActionEvent e)
    > {
    > if (Y && Z)
    > {
    > raiseEvent();
    > }
    > }
    >/**
    > I want to raise my own events.
    >*/
    > public void raiseEvent()
    > {
    > // What goes here?
    > }
    >// other methods here... [including some to call raiseEvent()]
    > boolean Y;
    > boolean Z;
    >}
    >
    >TIA
    >Kent
    >
    >



  3. #3
    Kent Guest

    Re: Handling and Raising events

    Thanks. I'm sorta used to it ;-)

    What I envisioned was having my class, an animation/model, work in either a
    single-step mode or in continuous play mode. When in continuous play mode,
    the animation would be based on the output of a timer. When in single-step
    mode, it would advance based on clicks of a button. If I capture these
    events in a 'sychronizer' class, the 'sychronizer' class would contain the
    logic to decide whether the timer events got passed on or whether the button
    events did. I reasoned that the easy way to do this was to listen for these
    events and raise my own 'sychronizer' event when appropriate. Then my
    animation just needed to play/advance one step/event that it received from
    the 'sychronizer' class; No 'play mode; logic in the animation.

    I guess that I'm stuck on this: how**exactly** do I raise an event? Do you
    have a simple example of raising an event?

    Kent

    "Kent (2)" <kb@essential.com.au> wrote in message
    news:3deabc9a$1@tnews.web.devx.com...
    >
    > Nice name :-)
    >
    > Your class will need to maintain a set of event listeners. These listeners
    > should be configurable via methods such as:
    >
    > void addActionListener(ActionListener listener);
    >
    > ActionListener removeActionListener(ActionListener listener);
    >
    > Assume the listeners are in a private ArrayList called actionListeners:
    >
    > private void raiseEvent() {
    > ActionEvent event = ...; //construct appropriate event here
    >
    > for (int i = 0; i < actionListeners.size(); ++i) {
    > ((ActionListener) actionListeners.get(i)).actionPerformed(event);
    > //invoke each actionPerformed() method on every listener
    > }
    > }
    >
    > Notice I declared raiseEvent() as private. This is because your class

    should
    > be responsible for invoking this not anyone else.
    >
    > Hope that helps,
    > Kent
    >
    > "Kent" <file_delete@hotmail.com> wrote:
    > >I have a Synchronizer class. I want it to raise an event every X seconds

    > if
    > >conditions Y & Z are True. Here's what I have already:
    > >
    > >public class Synchronizer implements ActionListener
    > >{
    > > timer Synchr; //swing timer
    > >
    > > public Synchronizer(int interval)
    > > {
    > > timer Synchr = new timer(interval, this);
    > > }
    > >/**
    > > When the Y & Z set to true, the class will allow timer events to pass.
    > >*/
    > > public void actionPerformed(ActionEvent e)
    > > {
    > > if (Y && Z)
    > > {
    > > raiseEvent();
    > > }
    > > }
    > >/**
    > > I want to raise my own events.
    > >*/
    > > public void raiseEvent()
    > > {
    > > // What goes here?
    > > }
    > >// other methods here... [including some to call raiseEvent()]
    > > boolean Y;
    > > boolean Z;
    > >}
    > >
    > >TIA
    > >Kent
    > >
    > >

    >





  4. #4
    Kent Guest

    Re: Handling and Raising events


    An event is nothing more than a method call really. As you say, your Synchronizer
    class will need to know whether to raise events via the timer or via a user-initiated
    action (ie. button click). No matter why the event is being raised it should
    be raised in the same fashion.

    What you need to decide is whether you will use a built-in event type (ActionEvent
    is a good candidate) or define your own. Listeners of your event should implement
    an interface (ActionListener corresponds to the ActionEvent) and your Synchronizer
    should maintain a list of listeners. The only listener in your case will
    be the animating class so it knows when to update the animation.

    To actually 'raise' or 'post' an event all you do is create an instance of
    ActionEvent and call a method on each listener. This method is defined in
    the ActionListener interface and takes an argument of type ActionEvent.

    So when do you raise the event? Well, your Synchronizer class should track
    whether the timer is being used or is 'paused'. If it is being used then
    the event should be posted every time the timer is 'up'. If it isn't being
    used, you should have a method in your public interface that allows the event
    to be explicitly posted. You may want to set up your ActionEvent object slightly
    differently in each case to indicate why /how the event was raised.

    BTW, you can see examples of event raising and what-not in the JDK source.
    Just look at the swing code for javax.swing.AbstractButton for example.

    Regards,
    Kent


    "Kent" <file_delete@hotmail.com> wrote:
    >Thanks. I'm sorta used to it ;-)
    >
    >What I envisioned was having my class, an animation/model, work in either

    a
    >single-step mode or in continuous play mode. When in continuous play mode,
    >the animation would be based on the output of a timer. When in single-step
    >mode, it would advance based on clicks of a button. If I capture these
    >events in a 'sychronizer' class, the 'sychronizer' class would contain the
    >logic to decide whether the timer events got passed on or whether the button
    >events did. I reasoned that the easy way to do this was to listen for these
    >events and raise my own 'sychronizer' event when appropriate. Then my
    >animation just needed to play/advance one step/event that it received from
    >the 'sychronizer' class; No 'play mode; logic in the animation.
    >
    >I guess that I'm stuck on this: how**exactly** do I raise an event? Do you
    >have a simple example of raising an event?
    >
    >Kent
    >
    >"Kent (2)" <kb@essential.com.au> wrote in message
    >news:3deabc9a$1@tnews.web.devx.com...
    >>
    >> Nice name :-)
    >>
    >> Your class will need to maintain a set of event listeners. These listeners
    >> should be configurable via methods such as:
    >>
    >> void addActionListener(ActionListener listener);
    >>
    >> ActionListener removeActionListener(ActionListener listener);
    >>
    >> Assume the listeners are in a private ArrayList called actionListeners:
    >>
    >> private void raiseEvent() {
    >> ActionEvent event = ...; //construct appropriate event here
    >>
    >> for (int i = 0; i < actionListeners.size(); ++i) {
    >> ((ActionListener) actionListeners.get(i)).actionPerformed(event);
    >> //invoke each actionPerformed() method on every listener
    >> }
    >> }
    >>
    >> Notice I declared raiseEvent() as private. This is because your class

    >should
    >> be responsible for invoking this not anyone else.
    >>
    >> Hope that helps,
    >> Kent
    >>
    >> "Kent" <file_delete@hotmail.com> wrote:
    >> >I have a Synchronizer class. I want it to raise an event every X seconds

    >> if
    >> >conditions Y & Z are True. Here's what I have already:
    >> >
    >> >public class Synchronizer implements ActionListener
    >> >{
    >> > timer Synchr; //swing timer
    >> >
    >> > public Synchronizer(int interval)
    >> > {
    >> > timer Synchr = new timer(interval, this);
    >> > }
    >> >/**
    >> > When the Y & Z set to true, the class will allow timer events to pass.
    >> >*/
    >> > public void actionPerformed(ActionEvent e)
    >> > {
    >> > if (Y && Z)
    >> > {
    >> > raiseEvent();
    >> > }
    >> > }
    >> >/**
    >> > I want to raise my own events.
    >> >*/
    >> > public void raiseEvent()
    >> > {
    >> > // What goes here?
    >> > }
    >> >// other methods here... [including some to call raiseEvent()]
    >> > boolean Y;
    >> > boolean Z;
    >> >}
    >> >
    >> >TIA
    >> >Kent
    >> >
    >> >

    >>

    >
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center