aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to write my own Event ( & Listener for it)?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to write my own Event ( & Listener for it)??" Watch "How to write my own Event ( & Listener for it)??" New topic
Author

How to write my own Event ( & Listener for it)??

Kevin Onik
Ranch Hand

Joined: Sep 10, 2003
Posts: 30
Hi everyone.
How can I write my own event? I mean like this :
-------------------------------------------
public class MyEventDemo ... implements MyEventListener {
...
//where initialization occurs:
//Register for MyEvent events on blankArea
blankArea.addMyEventListener(this);
}
...
public void myEventFired(MyEvent e) {
System.out.println("Thank God! ... MyEvent fired!!! ");
}
}
------------------------------------------

And I'm sure I don't want to use Observer...
Thanks.


"The greatest pleasure in life is doing what people say you cannot do." --Walter Bagehot<p>SCJP 1.4<br />SCWCD 1.3</p>
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
What are you trying to do in the larger sense? Are you trying to implement your own callback system? Are you trying to tap into the swing event handler?

Regards,
Aaron R>
Kevin Onik
Ranch Hand

Joined: Sep 10, 2003
Posts: 30
Hi Aaron,
I'm programming a set of components. For example about the DateField, (I'm coding now ) I want to fire a <B>DateChangedToBefore2000</B> event and
let the user to notified by it & perhaps handle it with my own component's event handler ( like this thig we do by LostFocuse in a JTextField! )! It's simple isn't it? ...
But I don't know how to do this!!!

please help me if you can.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2579

Have a look at Observable and Observer.

- Manish
Kevin Onik
Ranch Hand

Joined: Sep 10, 2003
Posts: 30
Originally posted by Manish Hatwalne:
Have a look at Observable and Observer.

- Manish


Thanks Manish ... But I mentioned before that
And I'm sure I don't want to use Observer...
Thanks.



Thank you :roll:
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Look at the javadoc for EventListenerList and EventObject.
D.

fyi, this is an implementation of an observable pattern - this is how events work in Swing.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Or you could use java.beans.PropertyChangeSupport/PropertyChangeEvent and most of the work is done for you.


SCJP2. Please Indent your code using UBB Code
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
Take a look at this receipt:


1. Create the class MyEvent. It must extend EventObject.

//------------------------------------------------------------
//file MyEvent.java
import java.util.EventObject;
public class MyEvent extends EventObject {
public MyEvent(Object source) {
super(source);
}
}
//_______________________end file


2. Create the listener class. It must extend EventListener.
//------------------------------------------------------------
//file MyEventListener.java

public interface MyEventListener extends EventListener {

public void myEventOccurred(MyEvent evt);

}
//_______________________end file




3. Add the notification code to the class that FIRES the event.
//------------------------------------------------------------
//file MyClass.java
//MyClass could be a home-made button

public class MyClass {

// Create the listener list

protected javax.swing.event.EventListenerList listenerList =
new javax.swing.event.EventListenerList();

// This methods allows classes to register for MyEvents

public void addMyEventListener(MyEventListener listener) {
listenerList.add(MyEventListener.class, listener);
}

// This methods allows classes to unregister for MyEvents

public void removeMyEventListener(MyEventListener listener) {
listenerList.remove(MyEventListener.class, listener);
}

// This private method is used to process the firing of MyEvents

void fireMyEvent() {
//1. GET ALL THE LISTENERS
Object[] listeners = listenerList.getListenerList();
//2. SEARCH THE APPROPRIATE LISTENER
//(= the one that implements MyEventListener)
// Each listener occupies two elements -
//the first is the listener class
// and the second is the listener instance!
MyEventListener listener = null;
for (int i=0; i<listeners.length; i+=2) {
if (listeners[i]==MyEventListener.class){
listener = (MyEventListener)listeners[i+1]);//i+1!!!
}
};
//3. Create an event for the appropriate listener
MyEvent event = new MyEvent(listener);
//4. Send that event to the listener
listener.myEventOccurred(evt);
}




//somewhere in a method an event (MyEvents) is fired
someMethod(){
//...
fireMyEvent();
//...
}

//------------------------------------------------------------



4. Add the event registration to the class that is Customer

//---------------------------------------------------------------------

//file Motherclass.java
//motherclass could be a JFrame

public class MotherClass implements MyEventListener{

MotherClass(){
MyClass myClass = new MyClass();
myClass.addMyEventListener(this);
//....
}

public void myEventOccurred(MyEvent e) {
if (e.getSource()==myClass){
//yep! event is fired
}
}
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In the example provided by EvenListenerList API only once EventObject is created. It is lazily initialized, and the source object is the class firing the event; not the class receiving the event notification.
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
I gues it should be like this:
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This is the example.
Kevin Onik
Ranch Hand

Joined: Sep 10, 2003
Posts: 30
Thank you so much!!! Very helpful
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
is there any way to generate events like throwing Exceptions?

like throw new Exception();

is there any own keyword for generating Events and processing them?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to write my own Event ( & Listener for it)??