Java: Example - Simple Editor

This simple text editor uses a JTextArea with Actions to implement the menu items. Actions are a good way to implement ActionListeners.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 
102 
103 
// editor/NutPad.java -- A very simple text editor -- Fred Swartz - 2004-08
// Illustrates use of AbstractActions for menus.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class NutPad extends JFrame {
    //-- Components 
    private JTextArea mEditArea;
    private JFileChooser mFileChooser = new JFileChooser(".");
    
    //-- Actions
    private Action mOpenAction;
    private Action mSaveAction;
    private Action mExitAction;
    
    //===================================================================== main
    public static void main(String[] args) {
        new NutPad().setVisible(true);
    }//end main
    
    //============================================================== constructor
    public NutPad() {
        createActions();
        this.setContentPane(new contentPanel());
        this.setJMenuBar(createMenuBar());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("NutPad");
        this.pack();
    }//end constructor

    ///////////////////////////////////////////////////////// class contentPanel
    private class contentPanel extends JPanel {        
        //========================================================== constructor
        contentPanel() {
            //-- Create components.
            mEditArea = new JTextArea(15, 80);
                mEditArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
                mEditArea.setFont(new Font("monospaced", Font.PLAIN, 14));
            JScrollPane scrollingText = new JScrollPane(mEditArea);
            
            //-- Do layout
            this.setLayout(new BorderLayout());
            this.add(scrollingText, BorderLayout.CENTER);
        }//end constructor
    }//end class contentPanel
    
    //============================================================ createMenuBar
    /** Utility function to create a menubar. */
    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = menuBar.add(new JMenu("File"));
                fileMenu.add(mOpenAction);  // Note use of actions, not text.
                fileMenu.add(mSaveAction);
                fileMenu.addSeparator(); 
                fileMenu.add(mExitAction);
        return menuBar;
    }//end createMenuBar
    
    //============================================================ createActions
    /** Utility function to define actions. */
    private void createActions() {
        mOpenAction = new AbstractAction("Open...") {
            public void actionPerformed(ActionEvent e) {
                int retval = mFileChooser.showOpenDialog(NutPad.this);
                if (retval == JFileChooser.APPROVE_OPTION) {
                    File f = mFileChooser.getSelectedFile();
                    try {
                        FileReader reader = new FileReader(f);
                        mEditArea.read(reader, "");  // Use TextComponent read
                    } catch (IOException ioex) {
                        System.out.println(e);
                        System.exit(1);
                    }
                }
            }
        };
        
        mSaveAction = new AbstractAction("Save") {
            public void actionPerformed(ActionEvent e) {
                int retval = mFileChooser.showSaveDialog(NutPad.this);
                if (retval == JFileChooser.APPROVE_OPTION) {
                    File f = mFileChooser.getSelectedFile();
                    try {
                        FileWriter writer = new FileWriter(f);
                        mEditArea.write(writer);  // Use TextComponent write
                    } catch (IOException ioex) {
                        System.out.println(e);
                        System.exit(1);
                    }
                }
            }
        };
        
        mExitAction = new AbstractAction("Exit") {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };
    }//end createActions    
}//end class NutPad