3

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

  1. what is sun.awt.windows.WToolkit?
  2. why am I able to see two events on a single mouse press?
JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
  • 1
    Consider using `swing` instead of `AWT` to program a GUI. [Why Swing is better than AWT](http://stackoverflow.com/questions/408820/what-is-the-difference-between-swing-and-awt#answer-408830) – BackSlash Feb 03 '14 at 10:09
  • By the way, even Swing is in maintenance-mode. If you are a beginner learning to do GUIs in Java, consider the successor to Swing: JavaFX/OpenJFX. – Basil Bourque Oct 07 '22 at 20:18

2 Answers2

3

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Holger
  • 285,553
  • 42
  • 434
  • 765
  • Ok. But I wasn't able to understand how is it showed as an event source? – JavaTechnical Feb 03 '14 at 10:01
  • 1
    I would also add that `sun.awt.windows.WToolkit` is an implementation for the Window platform on Windows. If you run that code on linux you get `sun.awt.X11.XToolkit`, which is the same thing but changes name according to the window manager :) – BackSlash Feb 03 '14 at 10:02
  • Nice. When press and release happened at the same time, it is considered a click. Who detects it as a click? The logical device driver for mouse? – JavaTechnical Feb 03 '14 at 10:05
  • @JavaTechnical The button is painted on the `Frame`, so if you click it you also click on the frame :) If you don't want the frame to fire that event, attach an `ActionListener` to the `Button` instead of extending system event queue – BackSlash Feb 03 '14 at 10:06
  • Ok. Here as the event is not controlled by the Button, the event moved up the hierarchy to the Frame? – JavaTechnical Feb 03 '14 at 10:06
  • @JavaTechnical I think so, but I'm not sure. It's the only reason I can think of. – BackSlash Feb 03 '14 at 10:08
  • That means the AWT 1.0 Delegation model is followed if a listener is not added to the Button to detect that event? – JavaTechnical Feb 03 '14 at 10:08
  • @JavaTechnical you really should start looking at the *type* of the event before starting speculations. There are more than just mouse events. – Holger Feb 03 '14 at 10:10
  • @JavaTechnical: As already said, just do a `System.out.println(theEvent);` instead of just printing the source only. Then you will start to understand, well hopefully. The events having the toolkit source are *not* mouse events. – Holger Feb 03 '14 at 10:15
  • I've found InvocationEvent being generated by the GlobalCursorManager on sun.awt.windows.WToolkit. I came to know that the GlobalCursorManager gets a component at a particular mouse point. But I didn't understand the role of the WToolkit. What is meant by **on sun.awt.windows.WToolkit** – JavaTechnical Feb 03 '14 at 10:27
  • You already know that. The Toolkit is the source of the event. Don’t try to interpret further meaning to it. The source is just a property of the event. – Holger Feb 03 '14 at 10:44
  • Ok. But why is WToolkit a source here? A `Button` is a source when mouse is pressed on it or some action is performed on it. But where is WToolkit in my program? How has it been the source? – JavaTechnical Feb 04 '14 at 11:04
  • It’s an object. It (or one of its helper objects) did create that event instance and so it is specified as the source of the event. It’s really simple. It’s really meaningless. There could be *any* object stored as the event’s source. The reason why a source is provided is because events insist on having a source. I really don’t get what your problem is. Are you trying to add philosophical meaning to the event source? – Holger Feb 04 '14 at 11:57
  • Ok that means the source here need not necessarily be a component object. Right? – JavaTechnical Feb 07 '14 at 05:37
  • @JavaTechnical: That’s right, the source of an `AWTEvent` does not need to be a `Component`. There is a subclass of it, `ComponentEvent`, which is used for events regarding a single `Component`. It consequently offers the source already cast to `Component` via [`getComponent()`](http://docs.oracle.com/javase/7/docs/api/java/awt/event/ComponentEvent.html#getComponent()). Note that [`ContainerEvent`, `FocusEvent`, `InputEvent`, `PaintEvent`, `WindowEvent` are sub-classes of `ComponentEvent`](http://docs.oracle.com/javase/7/docs/api/java/awt/event/package-tree.html). – Holger Feb 07 '14 at 09:28
0

You're implementing ALL of the libraries by using the * character. So the output specifies where the source of event occurred.

Space Ghost
  • 765
  • 2
  • 13
  • 26