Java by API/java.awt/AWTEvent
AWTEvent.MOUSE_EVENT_MASK
// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski. Copyright (c) 1997 O"Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.ruponent;
import java.awt.Frame;
import java.awt.ItemSelectable;
import java.awt.SystemColor;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
class ItemEventComponent extends Component implements ItemSelectable {
boolean selected;
int i = 0;
ItemListener itemListener = null;
ItemEventComponent() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Object[] getSelectedObjects() {
Object o[] = new Object[1];
o[0] = new Integer(i);
return o;
}
public void addItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.add(itemListener, l);
}
public void removeItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.remove(itemListener, l);
}
public void processEvent(AWTEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
if (itemListener != null) {
selected = !selected;
i++;
itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
getSelectedObjects(), (selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED)));
}
}
}
}
public class MainClass extends Frame implements ItemListener {
MainClass() {
super("Listening In");
ItemEventComponent c = new ItemEventComponent();
add(c, "Center");
c.addItemListener(this);
c.setBackground(SystemColor.control);
setSize(200, 200);
}
public void itemStateChanged(ItemEvent e) {
Object[] o = e.getItemSelectable().getSelectedObjects();
Integer i = (Integer) o[0];
System.out.println(i);
}
public static void main(String args[]) {
MainClass f = new MainClass();
f.show();
}
}
AWTEvent.WINDOW_EVENT_MASK
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ruponentEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main implements AWTEventListener {
public void eventDispatched(AWTEvent evt) {
if (evt.getID() == WindowEvent.WINDOW_OPENED) {
ComponentEvent cev = (ComponentEvent) evt;
if (cev.getComponent() instanceof JFrame) {
System.out.println("event: " + evt);
JFrame frame = (JFrame) cev.getComponent();
loadSettings(frame);
}
}
}
public static void loadSettings(JFrame frame) {
System.out.println("loading");
}
public static void saveSettings() {
System.out.println("saving");
}
public static void main(String[] args) throws Exception {
Toolkit tk = Toolkit.getDefaultToolkit();
final Main main = new Main();
tk.addAWTEventListener(main, AWTEvent.WINDOW_EVENT_MASK);
final JFrame frame = new JFrame("");
frame.setName("your frame");
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new AbstractAction("Quit") {
public void actionPerformed(ActionEvent evt) {
try {
main.saveSettings();
System.exit(0);
} catch (Exception ex) {
System.out.println(ex);
}
}
});
mb.add(menu);
frame.setJMenuBar(mb);
frame.pack();
frame.setVisible(true);
}
}