FileDocCategorySizeDatePackage
ListenerReuse.javaAPI DocExample1018Sat Nov 25 12:54:16 GMT 2000None

ListenerReuse

public class ListenerReuse extends Frame

Fields Summary
Constructors Summary
public ListenerReuse()

		Button b = new Button("Save");
		add(b);
		MenuBar mb = new MenuBar();
		setMenuBar(mb);
		Menu fm = new Menu("File");
		mb.add(fm);
		MenuItem mi = new MenuItem("Save");
		fm.add(mi);

		// Construct the ActionListener, and keep a reference to it.
		ActionListener saver = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("Saving your file...");
				// In real life we would call the doSave() method 
				// in the main class, something like this:
				// mainProg.doSave();
			}
		};
		// Register the actionListener with the Button
		b.addActionListener(saver);
		// And now register the same actionListener with the MenuItem
		mi.addActionListener(saver);
		pack();
	
Methods Summary
public static voidmain(java.lang.String[] a)
Main just calls the above

		ListenerReuse lr = new ListenerReuse();
		lr.setVisible(true);