• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Observable and Observer.

- Manish
 
Kevin Onik
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use java.beans.PropertyChangeSupport/PropertyChangeEvent and most of the work is done for you.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gues it should be like this:
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the example.
 
Kevin Onik
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much!!! Very helpful
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!