Java/Event/Customized Event
Содержание
ActionMap javax.swing.JComponent.getActionMap()
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
public class Main {
public static void main(String[] argv) throws Exception {
JButton component = new JButton();
MyAction action = new MyAction();
component.getActionMap().put(action.getValue(Action.NAME), action);
}
}
class MyAction extends AbstractAction {
public MyAction() {
super("my action");
}
public void actionPerformed(ActionEvent e) {
System.out.println("hi");
}
}
Creating a Custom Event
import java.util.EventListener;
import java.util.EventObject;
import javax.swing.event.EventListenerList;
class MyEvent extends EventObject {
public MyEvent(Object source) {
super(source);
}
}
interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt);
}
class MyClass {
protected EventListenerList listenerList = new EventListenerList();
public void addMyEventListener(MyEventListener listener) {
listenerList.add(MyEventListener.class, listener);
}
public void removeMyEventListener(MyEventListener listener) {
listenerList.remove(MyEventListener.class, listener);
}
void fireMyEvent(MyEvent evt) {
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i = i+2) {
if (listeners[i] == MyEventListener.class) {
((MyEventListener) listeners[i+1]).myEventOccurred(evt);
}
}
}
}
public class Main {
public static void main(String[] argv) throws Exception {
MyClass c = new MyClass();
c.addMyEventListener(new MyEventListener() {
public void myEventOccurred(MyEvent evt) {
System.out.println("fired");
}
});
}
}
Customized Event
import java.awt.AWTEvent;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.EventListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class CustomEventPanel extends JPanel implements TimerListener {
private int currentValue = 10;
JProgressBar bar = new JProgressBar(1,100);
public CustomEventPanel() {
TimerComponent t = new TimerComponent(1000);
t.addTimerListener(this);
add(bar);
}
public void timeElapsed(TimerEvent evt) {
currentValue += 10;
bar.setValue(currentValue);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Customized Event");
frame.setSize(300, 80);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new CustomEventPanel());
frame.show();
}
}
interface TimerListener extends EventListener {
public void timeElapsed(TimerEvent evt);
}
class TimerComponent extends Component implements Runnable {
private int interval;
private TimerListener listener;
private static EventQueue evtq;
public TimerComponent(int i) {
interval = i;
Thread t = new Thread(this);
t.start();
evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
enableEvents(0);
}
public void addTimerListener(TimerListener l) {
listener = l;
}
public void run() {
while (true) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
}
TimerEvent te = new TimerEvent(this);
evtq.postEvent(te);
}
}
public void processEvent(AWTEvent evt) {
if (evt instanceof TimerEvent) {
if (listener != null)
listener.timeElapsed((TimerEvent) evt);
} else
super.processEvent(evt);
}
}
class TimerEvent extends AWTEvent {
public static final int TIMER_EVENT = AWTEvent.RESERVED_ID_MAX + 5555;
public TimerEvent(TimerComponent t) {
super(t, TIMER_EVENT);
}
}
implements AWTEventListener
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);
}
}
int java.awt.AWTEvent.getID()
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);
}
}
Removing listeners: remove the registered listeners.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RemoveListener {
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(++count);
}
}
JButton add = new JButton("+");
JCheckBox active = new JCheckBox("Active listener");
ButtonListener buttonlistener = new ButtonListener();
int count = 0;
public RemoveListener() {
JPanel panel = new JPanel();
active.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (active.isSelected()) {
add.addActionListener(buttonlistener);
} else {
add.removeActionListener(buttonlistener);
}
}
});
panel.add(add);
panel.add(active);
JFrame f = new JFrame();
f.add(panel);
f.setSize(310, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new RemoveListener();
}
}