JavaTechnical
JavaTechnical

Reputation: 9357

What is sun.awt.windows.WToolkit?

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?

Upvotes: 3

Views: 4781

Answers (2)

Holger
Holger

Reputation: 298469

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.

Upvotes: 3

Space Ghost
Space Ghost

Reputation: 765

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

Upvotes: 0

Related Questions