[java]AWTEventMulticaster - マルチキャストイベントディスパッチを行う仕組み
2009/01/19
awt
java
Javaです。AWTEventMulticasterです。
AWTEventMulticaster は、java.awt.event パッケージで定義される AWT イベントに、効率的で、スレッドセーフな、マルチキャストイベントディスパッチを行う仕組みを実装します。javadoc
AWTEventMulticasterを使うと、Toolkitを使ってイベントひろってくれますね。
Swingでは、EventListenerListを使うようですけど、これだとjava.awt.Toolkitでイベントひろえないようなんだよなぁ。。。
package awtevent;
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
public static void main(String[] args) {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
System.out.println("*** " + event);
}
}, 0xffffffff);
a();
}
static void a() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("*** a:[" + e.getActionCommand() + "]");
}
};
actionListener = AWTEventMulticaster.add(actionListener,
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("*** b:[" + e.getActionCommand()
+ "]");
}
});
actionListener
.actionPerformed(new ActionEvent(new Object(), 7, "test"));
}
}
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
public static void main(String[] args) {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
System.out.println("*** " + event);
}
}, 0xffffffff);
a();
}
static void a() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("*** a:[" + e.getActionCommand() + "]");
}
};
actionListener = AWTEventMulticaster.add(actionListener,
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("*** b:[" + e.getActionCommand()
+ "]");
}
});
actionListener
.actionPerformed(new ActionEvent(new Object(), 7, "test"));
}
}
: