Notepad in Java

Introduction

 
In this article, I will introduce you to how to create a simple notepad in the Java programming language. This notepad contains the following functions: 
  • New
  • Open
  • Save
  • Save As
  • Undo
  • Cut
  • Paste
  • Copy
  • Delete
  • Select All
  • Status Bar
After extracting the preceding file, just execute the notepad with the name "call". Since the main class is named "call" in my notepad source code. 
 
Packages imported
 
The packages that are included in this application are: 
  • java.io.*;
  • java.awt.*;
  • java.awt.event.*;
  • javax.swing.*;
Listeners Used
  • Action Listener:
    This Listener is used when an Action is performed on any of the components, like when a button is clicked etcetera.
  • Window Listener:
    When a task is performed on the window this listener is used, it includes the following seven functions:
    1. public void windowActivated(WindowEvent e)  
    2. public void windowDeactivated(WindowEvent e)  
    3. public void windowIconified(WindowEvent e)  
    4. public void windowDeiconified(WindowEvent e)  
    5. public void windowClosed(WindowEvent e)  
    6. public void windowClosing(WindowEvent e)  
    7. public void windowClosing(WindowEvent e)  
  • Mouse Listener:
    This listener is used when a mouse action is performed, it includes the following methods in it:
    1. public void mouseEntered(MouseEvent e)  
    2. public void mouseExited(MouseEvent e)  
    3. public void mouseClicked(MouseEvent e)  
    4. public void mousePressed(MouseEvent e)  
    5. public void mouseReleased(MouseEvent e)   
Some important Functions
  1. Saving a File: This is a menu item of the File menu. The code of this menu is shown below:
    1. if (e.getSource() == mi3)   
    2. {  
    3.  fd2.setVisible(true);  
    4.  try   
    5.  {  
    6.   String pq;  
    7.   pq = ta.getText();  
    8.   byte b[] = pq.getBytes();  
    9.   fo = new FileOutputStream(fd2.getDirectory() + fd2.getFile());  
    10.   fo.write(b);  
    11.   fo.close();  
    12.  }   
    13.  catch (Exception ee) {}  
    14. }  
  1. Save As: When a user wants to save the file with some other name then the user clicks on the "Save As" option. The code for this function is as follows:
    1. if (e.getSource() == mi4)   
    2. {  
    3.  fd2.setVisible(true);  
    4.  try   
    5.  {  
    6.   String pq;  
    7.   pq = ta.getText();  
    8.   byte b[] = pq.getBytes();  
    9.   fo = new FileOutputStream(fd2.getDirectory() + fd2.getFile());  
    10.   fo.write(b);  
    11.   fo.close();  
    12.  }   
    13.  catch (Exception ee) {}  
    14. }  
  1. Open: To open the existing file, the user clicks on the "Open" menu item. The code for this open function is shown below: 
    1. if (e.getSource() == mi2)   
    2. {  
    3.  fd1.setVisible(true);  
    4.  try   
    5.  {  
    6.   fi = new FileInputStream(fd1.getDirectory() + fd1.getFile());  
    7.   DataInputStream ds = new DataInputStream(fi);  
    8.   String nm;  
    9.   while ((nm = ds.readLine()) != null)   
    10.   {  
    11.    ta.append(nm + String.valueOf((char10));  
    12.   }  
    13.   fi.close();  
    14.  }   
    15.  catch (Exception ee) {}  
    16. }  
  1. New: When the user wants to open a new file, this menu item is clicked on. The code for this is: 
    1. if (e.getSource() == mi1)   
    2. {  
    3.  ta.setText("");  
    4. }   
Command Used for Executing this File is:
 
"java call"
 
Output
 
The Notepad looks like this:
 
note.jpg


Similar Articles