---
category: swing
folder: EventListener
title: EventListenerを実装して独自イベント作成
tags: [EventListener, EventListenerList]
author: aterai
pubdate: 2004-01-26
description: イベント(イベントオブジェクト、イベントリスナー、イベントソース)を新たに作成し、これを使用します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMNwgwo5I/AAAAAAAAAY0/lpZGrcgRE8g/s800/EventListener.png
---
* Summary [#summary]
イベント(イベントオブジェクト、イベントリスナー、イベントソース)を新たに作成し、これを使用します。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMNwgwo5I/AAAAAAAAAY0/lpZGrcgRE8g/s800/EventListener.png)
* Source Code Examples [#sourcecode]
#code(link){{
interface FontChangeListener extends EventListener {
public void fontStateChanged(FontChangeEvent e);
}
class FontChangeEvent extends EventObject {
private final String command;
private final Font font;
public String getCommand() {
return command;
}
public Font getFont() {
return font;
}
public FontChangeEvent(Object source, String cmd, Font font) {
super(source);
this.command = cmd;
this.font = font;
}
}
// ...
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 e = list.elements();
FontChangeEvent evt = new FontChangeEvent(this, cmd, font);
while (e.hasMoreElements()) {
FontChangeListener listener = (FontChangeListener) e.nextElement();
listener.fontStateChanged(evt);
}
revalidate();
}
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは`JMenuItem`に設定したイベントでコンポーネントのフォントを変更できます。ラベルとボタンをその独自イベントのリスナーとして追加しているので、`fireFontChangeEvent(...)`メソッド内でそれらのフォントサイズが変更可能です。
`Java`のイベントモデルは、`delegation event model`(委譲型のイベントモデル)です。
----
リスナーの保存に`Vector`ではなく`EventListenerList`を使用する場合は[https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/event/EventListenerList.html EventListenerList]ドキュメントのサンプルが参考になります。
#code{{
// https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/event/EventListenerList.html
EventListenerList listenerList = new EventListenerList();
// FontChangeEvent fontChangeEvent = null;
public void addFontChangeListener(FontChangeListener l) {
listenerList.add(FontChangeListener.class, l);
}
public void removeFontChangeListener(FontChangeListener l) {
listenerList.remove(FontChangeListener.class, l);
}
// Notify all listeners that have registered interest for
// notification on this event type.The event instance
// is lazily created using the parameters passed into
// the fire method.
protected void fireFontChangeEvent(String cmd, Font font) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
FontChangeEvent evt = new FontChangeEvent(this, cmd, font);
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == FontChangeListener.class) {
// Lazily create the event:
// if (fontChangeEvent == null)
// fontChangeEvent = new FontChangeEvent(this);
((FontChangeListener) listeners[i + 1]).fontStateChanged(evt);
}
}
}
}}
* Reference [#reference]
- %%[http://www.asahi-net.or.jp/~dp8t-asm/java/tips/Event.html イベント]%%
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/event/EventListenerList.html EventListenerList (Java Platform SE 8)]
- [http://d.hatena.ne.jp/Kazzz/20080618/p1 習慣の生き物 - Kazzzの日記]
* Comment [#comment]
#comment
- `EventListenerList`を使用する方法を追加、リンクを追加、整理。 -- &user(aterai); &new{2008-06-18 (水) 12:57:22};
#comment