Menus (using Eclipse)

Main.java  [download]

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class Main extends JFrame {
	// Remember them in ivars for use in callback
	private JPanel contentPane;

	private MyData data; 
	private MyCanvas canvas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		// Holds our data
		data = new MyData ("Waiting for menu item");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		JMenuBar menuBar = new JMenuBar(); //*1 Create JMenuBar
		setJMenuBar(menuBar); //*2 Install JMenuBar in window

		JMenu mnFile = new JMenu("File"); //*3 Create JMenu and add it to menu bar
		menuBar.add(mnFile); //*3

		JMenuItem mntmOpen = new JMenuItem("Open"); //*4 Create JMenuItem and add it to its JMenu
		mntmOpen.addActionListener(new ActionListener() { //*5 Give each JMenuItem an ActionListener
				public void actionPerformed(ActionEvent e) { //*5
				menuCB(e); //*5
			}
		});
		mnFile.add(mntmOpen); //*4

		JMenuItem mntmSave = new JMenuItem("Save"); //*4
		mntmSave.addActionListener(new ActionListener() { //*5
			public void actionPerformed(ActionEvent e) {  //*5
				menuCB(e); //*5
			}
		});
		mnFile.add(mntmSave); //*4

		JMenuItem mntmQuit = new JMenuItem("Quit"); //*4
		mntmQuit.addActionListener(new ActionListener() { //*5
			public void actionPerformed(ActionEvent e) { //*5
				System.exit (0); //*8 Different callback for this one only
			}
		});
		mnFile.add(mntmQuit); //*4

		JMenu mnEdit = new JMenu("Edit"); //*3
		menuBar.add(mnEdit); //*3

		JMenuItem mntmCut = new JMenuItem("Cut"); //*4
		mntmCut.addActionListener(new ActionListener() { //*5
			public void actionPerformed(ActionEvent e) { //*5
				menuCB(e); //*5
			}
		});
		mnEdit.add(mntmCut); //*4

		JMenuItem mntmCopy = new JMenuItem("Copy"); //*4
		mntmCopy.addActionListener(new ActionListener() { //*5
			public void actionPerformed(ActionEvent e) { //*5
				menuCB(e); //*5
			}
		});
		mnEdit.add(mntmCopy); //*4

		JMenuItem mntmPaste = new JMenuItem("Paste"); //*4
		mntmPaste.addActionListener(new ActionListener() { //*5
			public void actionPerformed(ActionEvent e) { //*5
				menuCB(e); //*5
			}
		});
		mnEdit.add(mntmPaste); //*4

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		canvas = new MyCanvas(data); //*6 Manual tweak to pass arg
		contentPane.add(canvas, BorderLayout.CENTER);
	}

	// Common action for most of the pull-down menu items
	private void menuCB (ActionEvent e) { //*7 Manually coded callback
		// Change the text
		data.setText ("Menu item: " + e.getActionCommand()); //*7

		// Then trigger a redraw
		canvas.repaint (); //*7
	}
}

MyData.java  [download]

public class MyData { //*1 All manually coded
	private String text; //*2 Holds our data, canvas calls us for latest when redraws

	public MyData (String text) {
		this.text = text;
	}

	public void setText (String text) { //*2
		this.text = text; //*2
	}

	public String getText () { //*2
		return text; //*2
	}
}

MyCanvas.java  [download]

import java.awt.Graphics;

import javax.swing.JPanel;

public class MyCanvas extends JPanel { //*1 All manually coded
	// Save it for use in draw callback
	private MyData data; //*1

	public MyCanvas (MyData data) { //*1
		this.data = data; //*1
	}

	// This is our draw callback
	public void paintComponent (Graphics g) { //*1
		super.paintComponent(g); //*1
		String s = data.getText(); //*1
		g.drawString (s, 100, 100); //*1
	}

}