/* (@)ToolkitAWTListenerTestGUI.java */
/* Copyright 2009 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.AWTEvent;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
/**
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
*/
public class ToolkitAWTListenerTestGUI {
public static void main(String[] args) {
final JPanel contentPane = new JPanel(new GridLayout(2, 2, 6, 6));
contentPane.add(new JTextField(15));
contentPane.add(new JButton("Hi"));
contentPane.add(new JSpinner(new SpinnerNumberModel()));
contentPane.add(new JComboBox(new String[] { "A", "B", "C" }));
final long eventMask =
AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK
| AWTEvent.KEY_EVENT_MASK;
contentPane.getToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
final int id = event.getID();
if (id >= MouseEvent.MOUSE_FIRST && id <= MouseEvent.MOUSE_LAST) {
final Point point = ((MouseEvent) event).getLocationOnScreen();
System.out.println("Mouse: " + point.x + ":" + point.y);
} else if (id >= KeyEvent.KEY_FIRST && id <= KeyEvent.KEY_LAST) {
final KeyEvent keyEvent = (KeyEvent) event;
final int keyCode = keyEvent.getKeyCode();
System.out.println("Key: " + KeyEvent.getKeyText(keyCode));
}
}
}, eventMask);
final JFrame f = new JFrame("ToolkitAWTListenerTestGUI");
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}