FileDocCategorySizeDatePackage
Editor.javaAPI DocExample1710Sat Apr 23 22:35:38 BST 2005None

Editor

public class Editor extends JFrame implements ActionListener

Fields Summary
private JEditorPane
textPane
Constructors Summary
public Editor()

  
     
    super("Editor v1.0");
    Container content = getContentPane( ); // unecessary in 1.5+
    content.add(new JScrollPane(textPane), BorderLayout.CENTER);
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Quit"));
    JMenuBar menuBar = new JMenuBar( );
    menuBar.add(menu);
    setJMenuBar(menuBar);
    setSize(300, 300);
	setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

    String command = e.getActionCommand( );
    if (command.equals("Quit")) System.exit(0);
    else if (command.equals("Open")) loadFile( );
    else if (command.equals("Save")) saveFile( );
  
private voidloadFile()

    JFileChooser chooser = new JFileChooser( );
    int result = chooser.showOpenDialog(this);
    if (result == JFileChooser.CANCEL_OPTION) return;
    try {
      File file = chooser.getSelectedFile( );
      java.net.URL url = file.toURL( );
      textPane.setPage(url);
    }
    catch (Exception e) {
      textPane.setText("Could not load file: " + e);
    }
  
public static voidmain(java.lang.String[] s)

 
	new Editor().setVisible(true);
  
private javax.swing.JMenuItemmakeMenuItem(java.lang.String name)

    JMenuItem m = new JMenuItem( name );
    m.addActionListener( this );
    return m;
  
private voidsaveFile()

    JFileChooser chooser = new JFileChooser( );
    chooser.showSaveDialog(this);
    // Save file data...