Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2006, 2:27 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Time Listener

i was bored and i thought i'd try to make a custom event listener. yesterday i didn't know anything about making a cutom event listener (found it too complicated). today i tried and got the following code:

//
// TEST.java
//
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;

class TEST implements TimeListener
{
    public void timeChanged (TimeEvent e)
    {
        System.out.println ("Time Event Fired");
    }


    public TEST ()
    {
        addTimeListener (this);
    }


    static public void main (String[] args)
    {
        new TEST ();
    }


   EventListenerList listenerList = new EventListenerList ();

    public void addTimeListener (TimeListener l)
    {
        listenerList.add (TimeListener.class, l);
        Action al = new AbstractAction ()
        {
            public void actionPerformed (ActionEvent e)
            {
                fireTimeEvent (new TimeEvent (this));
            }
        }
        ;
        int delay = 1000;
        new Timer (delay, al).start ();
    }


    public void removeTimeListener (TimeListener l)
    {
        listenerList.remove (TimeListener.class, l);
    }


    void fireTimeEvent (TimeEvent e)
    {
        Object[] listeners = listenerList.getListenerList ();

        for (int i = 0 ; i < listeners.length ; i += 2)
        {
            if (listeners [i] == TimeListener.class)
            {
                ((TimeListener) listeners [i + 1]).timeChanged (e);
            }
        }
    }
}

//
// TimeEvent.java
//
import java.util.*;
public class TimeEvent extends EventObject
{
    public TimeEvent (Object source)
    {
        super (source);
    }
}

//
// TimeListener.java
//
import java.util.*;

public interface TimeListener extends EventListener
{
    public void timeChanged (TimeEvent e);
}

i'm not sure if time listener is the best name, but last year, i wanted to make an event that would wait for around 5 seconds and fire itself automatically. so that's why i called this time listener.

could someone give me comments on this.

thanks
Dark Flare Knight is offline   Reply With Quote
Old Apr 2nd, 2006, 10:34 PM   #2
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
Not that I know anything, but an event that fires after x seconds would simply be a timer. A time listener, to me, would fire an action when a certain time is observed, the same way an event listener fires whenever a given event occurs.

Which one were you going for?
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old Apr 3rd, 2006, 12:06 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 930
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I think threads would be your best bet for this sort of thing.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC