I have the following piece of code
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import javax.swing.*;
class QueueTest {
static int i=0;
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
EventQueue eventQueue =
Toolkit.getDefaultToolkit().getSystemEventQueue();
eventQueue.push(new MyEventQueue());
Frame f=new Frame();
f.setSize(400,400);
//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(150,150);
f.setLayout(new FlowLayout());
f.setVisible(true);
Button b=new Button("button");
//b.setEnabled(false);
f.add(b);
/*
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println("button is clicked");
}
});
*/
}
private static class MyEventQueue extends EventQueue {
public void postEvent(AWTEvent theEvent) {
// System.out.println("Event Posted");
System.out.println("The source of event is "+theEvent.getSource());
super.postEvent(theEvent);
}
protected void dispatchEvent(AWTEvent event)
{
System.out.println("The source of event ("+(i++)+") is
"+event.getSource());
super.dispatchEvent(event);
}
}
}
In the output i could sometimes see
The source of event is (78) sun.awt.windows.WToolkit@77ef83
when I think i have only two sources, the java.awt.Button and the java.awt.Frame. Also when I am pressing the mouse, I could see two events being generated for which one is sun.awt.windows.WToolkit is the source and for the other is Button (when I clicked on button).
My questions are
The names speak for themselves: AWT stands for Abstract Window Toolkit which implies that the Toolkit is abstract and requires an actual implementation. sun.awt.windows.WToolkit is such an implementation for the Microsoft Windows plattform hence the W in its name. On other operating systems you will see different implementations, e.g. sun.awt.X11.XToolkit on Linux. If you just do a System.out.println(Toolkit.getDefaultToolkit()); you will see that the Toolkit’s string representation matches that of the event source which you see from time to time.
I suggest you do a print of the entire event instead of just the source. Then you will see what these events are for. You will see what kind of events the toolkit generates. And you will see that a mouse click can generate up to three events: one for the press, one for the release and one if pressing and releasing happened at the same location which is considered a click.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With