![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I think threads would be your best bet for this sort of thing.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|