• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I gues it should be like this:
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the example.
 
Kevin Onik
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much!!! Very helpful
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic