• 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

概要

概要

イベント(イベントオブジェクト、イベントリスナー、イベントソース)を新たに作成し、これを使用します。

サンプルコード

サンプルコード

interface FontChangeListener extends EventListener {
  public void fontStateChanged(FontChangeEvent e);
}
#spandel
class FontChangeEvent extends EventObject{
#spanend
#spanadd

#spanend
#spanadd
class FontChangeEvent extends EventObject {
#spanend
  private final String command;
  private final Font font;
  public String getCommand() {
    return command;
  }
#spanadd

#spanend
  public Font getFont() {
    return font;
  }
#spanadd

#spanend
  public FontChangeEvent(Object source, String cmd, Font font) {
    super(source);
    this.command = cmd;
    this.font = font;
  }
}
#spandel
View in GitHub: Java, Kotlin
#spanend
#spanadd
// ...
#spanend
private final Vector listenerList = new Vector();
#spanadd

#spanend
public void addFontChangeListener(FontChangeListener l) {
  if (!listenerList.contains(l)) {
    listenerList.add(l);
  }
}
#spanadd

#spanend
public void removeFontChangeListener(FontChangeListener l) {
  listenerList.remove(l);
}
#spanadd

#spanend
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();
}

解説

上記のサンプルではメニューからのイベントでコンポーネントのフォントを変更しています。ラベルとボタンをリスナーとして追加しているので、fireFontChangeEventでそれらのフォントサイズが変更されます。

解説

上記のサンプルではJMenuItemに設定したイベントでコンポーネントのフォントを変更できます。ラベルとボタンをその独自イベントのリスナーとして追加しているので、fireFontChangeEvent(...)メソッド内でそれらのフォントサイズが変更可能です。 Javaのイベントモデルは、delegation event model(委譲型のイベントモデル)です。イベントなどを参考にしてみてください。 Javaのイベントモデルは、delegation event model(委譲型のイベントモデル)です。
Vectorではなく、EventListenerListを使用する場合は、EventListenerList (Java Platform SE 7)のサンプルが参考になります。 リスナーの保存にVectorではなくEventListenerListを使用する場合はEventListenerListドキュメントのサンプルが参考になります。
#spandel
// http://docs.oracle.com/javase/jp/6/api/javax/swing/event/EventListenerList.html
#spanend
#spanadd
// https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/event/EventListenerList.html
#spanend
EventListenerList listenerList = new EventListenerList();
#spandel
//FontChangeEvent fontChangeEvent = null;
#spanend
#spanadd
// FontChangeEvent fontChangeEvent = null;
#spanend
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);
    }
  }
}

参考リンク

参考リンク

コメント

コメント