Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- jsp
- TextBox
- 안드로이드
- asp.net
- html
- WebView
- Redirect
- 웹 서비스
- 웹뷰
- 자바스크립트
- C#
- 자바
- 이클립스
- Eclipse
- SpringSource Tool Suite
- varags
- 컬럼명
- Android
- javascript
- Java
- scrollView
- Bootstrap
- MSsql
- decompiler
- MS-SQL
- Web Service
- MANTIS
- STS
- Maven
- Apache Lucene
Archives
- Today
- Total
bboks.net™
Creating a Custom Event 본문
e333. Creating a Custom Event
Here's an example of how to register for MyEvents.
A new custom event must extends EventObject
. Moreover, an event listener interface must be declared to allow objects to receive the new custom event. All listeners must extend from EventListener
.
This example demonstrates all the steps necessary to create a new custom event.
// Declare the event. It must extend EventObject. public class MyEvent extends EventObject { public MyEvent(Object source) { super(source); } } // Declare the listener class. It must extend EventListener. // A class must implement this interface to get MyEvents. public interface MyEventListener extends EventListener { public void myEventOccurred(MyEvent evt); } // Add the event registration and notification code to a class. 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 class is used to fire MyEvents void fireMyEvent(MyEvent evt) { Object[] listeners = listenerList.getListenerList(); // Each listener occupies two elements - the first is the listener class // and the second is the listener instance for (int i=0; i<listeners.length; i+=2) { if (listeners[i]==MyEventListener.class) { ((MyEventListener)listeners[i+1]).myEventOccurred(evt); } } } }
Here's an example of how to register for MyEvents.
MyClass c = new MyClass(); // Register for MyEvents from c c.addMyEventListener(new MyEventListener() { public void myEventOccurred(MyEvent evt) { // MyEvent was fired } });
[출처] http://www.exampledepot.com/egs/java.util/CustEvent.html