#navi(../)
*EventListenerを実装して独自イベント作成 [#sc76994a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-01-26~
更新日:&lastmod;

#contents
**概要 [#z5c8b70a]
イベント(イベントオブジェクト、イベントリスナ、イベントソース)を新たに作成し、これを使用します。

**サンプルコード [#a2ffd4f4]
 private final Vector listenerList = new Vector();
 public void addFontChangeListener(FontChangeListener l) {
   if(!listenerList.contains(l)) listenerList.add(l);
 }
 public void removeFontChangeListener(FontChangeListener l) {
   listenerList.remove(l);
 }
 public void fireFontChangeEvent(String cmd, Font font) {
   Vector list = (Vector)listenerList.clone();
   Enumeration enum = list.elements();
   FontChangeEvent e = new FontChangeEvent(this, cmd, font);
   while(enum.hasMoreElements()) {
     FontChangeListener l = (FontChangeListener)enum.nextElement();
     l.fontStateChanged(e);
   }
   revalidate();
 }

-[[サンプルを起動>http://terai.xrea.jp/swing/eventlistener/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/eventlistener/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/eventlistener/src.zip]]
**解説 [#t8cad221]
上記のサンプルではメニューからのイベントでコンポーネントのフォントを変更しています。フォントの初期値はイベントリスナをimplementsしたクラスのコンストラクタで指定しています。

Javaのイベントモデルは、delegation event model(委譲型のイベントモデル)です。以下の参考リンクなどで詳細をチェックしてみてください。

**参考リンク [#v3fa59a9]
-[[イベント>http://www.asahi-net.or.jp/~dp8t-asm/java/tips/Event.html]]
-[[イベント処理 - delegation event model>http://www.gimlay.org/~javafaq/S065.html]]

**コメント [#i8717bdd]
#comment