Swing/EventListener のバックアップの現在との差分(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/EventListener へ行く。
- 1 (2004-06-02 (水) 09:53:01)
- 2 (2004-07-28 (水) 06:19:22)
- 3 (2004-08-31 (火) 12:34:09)
- 4 (2004-09-01 (水) 06:29:08)
- 5 (2004-10-08 (金) 06:19:41)
- 6 (2004-11-04 (木) 10:04:58)
- 7 (2005-02-03 (木) 02:03:58)
- 8 (2005-04-28 (木) 04:32:52)
- 9 (2005-05-11 (水) 00:27:23)
- 10 (2005-07-05 (火) 01:53:12)
- 11 (2005-11-06 (日) 21:58:43)
- 12 (2006-02-27 (月) 15:52:04)
- 13 (2006-04-12 (水) 19:43:34)
- 14 (2006-05-31 (水) 10:49:16)
- 15 (2006-06-08 (木) 16:04:58)
- 16 (2006-09-12 (火) 17:24:39)
- 17 (2007-08-10 (金) 12:33:49)
- 18 (2008-06-18 (水) 12:32:06)
- 19 (2013-04-02 (火) 19:18:23)
- 20 (2013-10-02 (水) 15:39:58)
- 21 (2014-12-19 (金) 14:15:54)
- 22 (2015-03-25 (水) 16:56:20)
- 23 (2015-03-28 (土) 15:51:47)
- 24 (2017-02-21 (火) 16:43:01)
- 25 (2018-01-03 (水) 19:50:06)
- 26 (2018-10-16 (火) 13:27:00)
- 27 (2020-10-13 (火) 18:28:00)
- 28 (2022-07-31 (日) 21:36:32)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:EventListenerを実装して独自イベント作成 #navi(../) *EventListenerを実装して独自イベント作成 [#sc76994a] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2004-01-26~ 更新日:&lastmod; --- 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] イベント(イベントオブジェクト、イベントリスナー、イベントソース)を新たに作成し、これを使用します。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMNwgwo5I/AAAAAAAAAY0/lpZGrcgRE8g/s800/EventListener.png) **概要 [#z5c8b70a] イベント(イベントオブジェクト、イベントリスナ、イベントソース)を新たに作成し、これを使用します。 * サンプルコード [#sourcecode] #code(link){{ interface FontChangeListener extends EventListener { public void fontStateChanged(FontChangeEvent e); } **サンプルコード [#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(); } class FontChangeEvent extends EventObject { private final String command; private final Font font; public String getCommand() { return command; } -&jnlp; -&jar; -&zip; public Font getFont() { return font; } **解説 [#t8cad221] 上記のサンプルではメニューからのイベントでコンポーネントのフォントを変更しています。フォントの初期値はイベントリスナをimplementsしたクラスのコンストラクタで指定しています。 public FontChangeEvent(Object source, String cmd, Font font) { super(source); this.command = cmd; this.font = font; } } // ... private final Vector listenerList = new Vector(); Javaのイベントモデルは、delegation event model(委譲型のイベントモデル)です。以下の参考リンクなどで詳細をチェックしてみてください。 public void addFontChangeListener(FontChangeListener l) { if (!listenerList.contains(l)) { listenerList.add(l); } } **参考リンク [#v3fa59a9] -[[イベント>http://www.asahi-net.or.jp/~dp8t-asm/java/tips/Event.html]] -[[イベント処理 - delegation event model>http://www.gimlay.org/~javafaq/S065.html]] public void removeFontChangeListener(FontChangeListener l) { listenerList.remove(l); } **コメント [#i8717bdd] 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(); } }} * 解説 [#explanation] 上記のサンプルでは`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] - %%[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 - `EventListenerList`を使用する方法を追加、リンクを追加、整理。 -- &user(aterai); &new{2008-06-18 (水) 12:57:22}; #comment