FileDocCategorySizeDatePackage
NotePad.javaAPI DocExample1875Wed Feb 20 14:28:50 GMT 2002None

NotePad

public class NotePad extends JFrame implements ActionListener

Fields Summary
private JFileChooser
chooser
private JMenuItem
newfile
private JMenuItem
open
private JMenuItem
close
Constructors Summary
public NotePad()

    super ("Menu Example");
    JMenuBar jmb = new JMenuBar();
    JMenu file = new JMenu ("File");
    
    file.add (newfile);
    newfile.addActionListener (this);
    
    file.add (open);
    open.addActionListener (this);
    file.addSeparator();
    
    file.add (close);
    close.addActionListener (this);
    jmb.add (file);

    setJMenuBar (jmb);
  
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

  
  
     
  System.out.println (e.getActionCommand());
  if(e.getSource() == open) {
   	// Display the file chooser.
	int retval = chooser.showDialog(this, null);
	// Get the file name (if the return value is correct)...
	if(retval == JFileChooser.APPROVE_OPTION) {
		// Retrieve the file name and make sure it isn't null.
	    File theFile = chooser.getSelectedFile();
	    if(theFile != null) {
		if(theFile.isDirectory()) {
				JOptionPane.showMessageDialog(this, "You chose this directory: " +
				chooser.getSelectedFile().getAbsolutePath());
			} else {
		    	JOptionPane.showMessageDialog(this, "You chose this file: " +
				chooser.getSelectedFile().getAbsolutePath());
			}		
			return;
		}
	}
	JOptionPane.showMessageDialog(this, "No file was chosen.");
  }
public static voidmain(java.lang.String[] args)

		System.out.println("Starting Notepad...");
		NotePad mainFrame = new NotePad();
		mainFrame.setSize(400, 400);
		mainFrame.setTitle("Menu demonstration");
		mainFrame.setVisible(true);