Simple Editor in Java

Introduction

This simple editor has only one menu with New, Open, Save, Exit option. It requires JMenuBar, JMenu, JMenuItem, ActionListener, File related classes. This tutorial demonstrate creation of menu and reading and writing the files. Once user clicks on open menu then Open dialog box is displayed and File is displayed in the display area. This simple editor can be created easily with following steps. File reading and writing part is similar to previous tutorial.

Implementation

Step 1: Designing the Menu The class is application so it extends JFrame. The constructor of the class contain the code for designing the menu and adding action listeners. We require to create menu by using JMenu which include different JMenuItem like Open, Save, New, Exit. JTextArea is used for editing and displaying the text.It is created to hold and display 15 lines and 80 characters per line. Short cut key for any action can set using setMnemonic( ) function. Menu Items are created with JMenuItem class follows.
  1.         JMenu fileMenu=new JMenu("File");  //create a menu
  2.         fileMenu.setMnemonic( 'F' ); // short cut key Alt + F
  3.         area=new JTextArea(15,80);  // area for editing and displaying  the text
  4.         JMenuItem saveItem=new JMenuItem("Save");
  5.         saveItem.setMnemonic( 'S' );
  6.         JMenuItem openItem=new JMenuItem("Open");
  7.         openItem.setMnemonic( 'O' );
  8.         JMenuItem newItem=new JMenuItem("New");
  9.         newItem.setMnemonic( 'N' );
  10.         JMenuItem exitItem=new JMenuItem("Exit");
  11.         exitItem.setMnemonic( 'Q' );
More items can be created as above. Step 2: Defining ActionListeners When a user selects one of the option, some action should be performed which is done by adding action listeners to the menu items. When a save menu item is selected the save dialog box is displayed using JFileChooser which allows to select a file. fileChooser object's showSaveDialog() method is used to select the file for saving the data, this method shows the Home directory location by default. It returns the option which was selected after choosing the file. If save was selected then APPROVE_OPTION is returned else CANCEL_OPTION is returned. getSelectedFile() method returns the file object which was selected. As in our previous tutorial FileWriter method write the data to the file.
  1.         saveItem.addActionListener(new ActionListener(){
  2.                 public void actionPerformed(ActionEvent e){
  3.                         JFileChooser fileChooser=new JFileChooser();
  4.                         fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  5.                         int result=fileChooser.showSaveDialog(MenuDemo.this);
  6.                         if(result==JFileChooser.APPROVE_OPTION)
  7.                         {
  8.                                 File f=fileChooser.getSelectedFile(); //get file selected by user
  9.                                 try{
  10.                                 FileWriter ostream=new FileWriter(f);
  11.                                 ostream.write(area.getText());
  12.                                 ostream.close();
  13.                                 }
  14.                                 catch(IOException ioe){
  15.                                 ioe.printStackTrace();
  16.                                
  17.                                 }
  18.                         }
  19.                 }      
  20.                 });
Similarly action listener is added to the open menu item as follows. FileReader and BufferedReader objects are used to read data from selected file. Details of this can be obtained from previous tutorial. BufferedReader's readLine() reads a line at a time and it is appended to the display area using JTextArea's append() method.
  1.         openItem.addActionListener(new ActionListener(){
  2.                 public void actionPerformed(ActionEvent e)
  3.                 {
  4.                         JFileChooser fileChooser=new JFileChooser();
  5.                         fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  6.                         int result=fileChooser.showOpenDialog(MenuDemo.this);
  7.                         if(result==JFileChooser.APPROVE_OPTION)
  8.                         {
  9.                                 File f=fileChooser.getSelectedFile();
  10.                                 try{
  11.                                 FileReader reader=new FileReader(f);
  12.                                 BufferedReader inStream=new BufferedReader(reader);
  13.                                 String line=inStream.readLine();
  14.                                 while(line!=null)
  15.                                 {
  16.                                         area.append(line + "\n");
  17.                                         line=inStream.readLine();
  18.                                 }
  19.                                 inStream.close();
  20.                                
  21.                                 }
  22.                                 catch (FileNotFoundException fe) {
  23.                
  24.                                 fe.printStackTrace();
  25.                        
  26.                                
  27.                                 }
  28.                                 catch (IOException ie) {
  29.                                 ie.printStackTrace();
  30.                        
  31.                                 }
  32.                         }
  33.                 }
New menu item simply clear the text area by setting empty text.
  1. newItem.addActionListener(new ActionListener(){
  2.         public void actionPerformed(ActionEvent e)     
  3.         {
  4.                 area.setText("");
  5.         }
  6.         });
Exit menu item exit's the editor.
  1. exitItem.addActionListener(new ActionListener(){
  2.                         public void actionPerformed(ActionEvent e){
  3.                                 System.exit(0);
  4.                         }
  5.                         });
  6.         });
Step 3: Adding the menu to the frame All the menu items are added to the menu and a menu bar is created using JMenuBar and above menu is added to it. JMenuBar's setMenuBar() method sets the menu bar for the frame. After this text area is added to the frame.
  1. fileMenu.add(newItem);
  2.         fileMenu.add(openItem);
  3.         fileMenu.add(saveItem);
  4.         fileMenu.add(exitItem);
  5.  
  6.         JMenuBar menuBar=new JMenuBar();
  7.         setJMenuBar(menuBar);
  8.         menuBar.add(fileMenu);
  9.  
  10.        
  11.         add(area);
  12.         setSize(400,400); //set size of frame
  13.         setVisible(true);
Step 4: Defining the main() method In the main method object of above class is created and some properties are set.
  1. public static void main(String[] args)
  2.         {
  3.                 Editor simpleEditor=new Editor();
  4.                 simpleEditor.setDefaultCloseOperation(EXIT_ON_CLOSE);
  5.         }

Add new comment