How to Create a Notepad Project in Java

Submitted by: 
Language: 
Visitors have accessed this post 66001 times.

Introduction:
This tutorial is on how to create a simple Notepad application in Java. The program will save and open files as well as have close and new functions.

Steps of Creation:
Step 1:

First we want to create two classes; Main and Grid. In our Main class we will have our JFrame (which is the window of the application). The JFrame will lead to our Grid class which will contain the Notepad code.

  1. import javax.swing.JFrame;
  2.  
  3. public class Main {
  4. public static void main(String args[]){
  5. JFrame frame = new Grid();
  6. frame.setTitle("Notepad");
  7. frame.setVisible(true);
  8. frame.setSize(1280, 720);
  9. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. frame.setResizable(true);
  11. frame.setLocationRelativeTo(null);
  12. }
  13. }

We are creating a window/JFrame with the title as "Notepad", size as 1280x720 and making it equal to our Grid class.

Step 2:
Now that we have our JFrame (run it to test it works, you should see a blank 1280x720 window). We want to create a few variables to swap between our save and open functions and our main constructor.

  1. int fileToOpen;
  2. int fileToSave;
  3. JFileChooser fileOpen;
  4. JFileChooser fileSave;

Step 3:
Lets first create our objects within our Grid constructor. We want a TextArea which is the area containing the file information and a MenuBar containing MenuItems (such as "Save").

We then create the MenuBar and attach our File MenuItem to the MenuBar, followed by creating and attaching further options in our File MenuItem.

  1. Grid(){
  2. MenuBar menuBar = new MenuBar();
  3. MenuItem menutem = new MenuItem();
  4. final JTextArea textArea = new JTextArea();
  5. setMenuBar(menuBar);
  6. Menu file = new Menu("File");
  7. menuBar.add(file);
  8. MenuItem newOption = new MenuItem("New");
  9. MenuItem open = new MenuItem("Open");
  10. MenuItem save = new MenuItem("Save");
  11. MenuItem close = new MenuItem("Exit");
  12. file.add(newOption);
  13. file.add(open);
  14. file.add(save);
  15. file.add(close);
  16. getContentPane().add(textArea);
  17.  
  18. newOption.addActionListener(new ActionListener(){
  19. public void actionPerformed(ActionEvent e){
  20. textArea.setText("");
  21. }
  22. });
  23.  
  24. open.addActionListener(new ActionListener(){
  25. public void actionPerformed(ActionEvent e){
  26. openFile();
  27. if (fileToOpen == JFileChooser.APPROVE_OPTION){
  28. textArea.setText("");
  29. try{
  30. Scanner scan = new Scanner(new FileReader(fileOpen.getSelectedFile().getPath()));
  31. while (scan.hasNext())
  32. textArea.append(scan.nextLine());
  33. } catch (Exception ex){
  34. System.out.println(ex.getMessage());
  35. }
  36. }
  37. }
  38. });
  39.  
  40. save.addActionListener(new ActionListener(){
  41. public void actionPerformed(ActionEvent e){
  42. saveFile();
  43. if (fileToSave == JFileChooser.APPROVE_OPTION){
  44. try {
  45. BufferedWriter out = new BufferedWriter(new FileWriter(fileSave.getSelectedFile().getPath()));
  46. out.write(textArea.getText());
  47. out.close();
  48. } catch (Exception ex) {
  49. System.out.println(ex.getMessage());
  50. }
  51. }
  52. }
  53. });
  54.  
  55. close.addActionListener(new ActionListener(){
  56. public void actionPerformed(ActionEvent e){
  57. System.exit(0);
  58. }
  59. });
  60. }

For each option we create a ActionListener and give the appropriate code for each.

Step 4:
The final things we need are our Open and Save File Functions...

  1. public void openFile(){
  2. JFileChooser open = new JFileChooser();
  3. int option = open.showOpenDialog(this);
  4. fileToOpen = option;
  5. fileOpen = open;
  6. }
  7.  
  8. public void saveFile(){
  9. JFileChooser save = new JFileChooser();
  10. int option = save.showOpenDialog(this);
  11. fileToSave = option;
  12. fileSave = save;
  13. }

Project Complete!
That's it! Below is the full code and download to the files. Thanks for reading!

Main:

  1. import javax.swing.JFrame;
  2.  
  3. public class Main {
  4. public static void main(String args[]){
  5. JFrame frame = new Grid();
  6. frame.setTitle("Notepad");
  7. frame.setVisible(true);
  8. frame.setSize(1280, 720);
  9. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. frame.setResizable(true);
  11. frame.setLocationRelativeTo(null);
  12. }
  13. }

Grid:

  1. import java.awt.Menu;
  2. import java.awt.MenuBar;
  3. import java.awt.MenuItem;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedWriter;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.util.Scanner;
  10.  
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JFrame;
  13. import javax.swing.JTextArea;
  14.  
  15. public class Grid extends JFrame{
  16. int fileToOpen;
  17. int fileToSave;
  18. JFileChooser fileOpen;
  19. JFileChooser fileSave;
  20. Grid(){
  21. MenuBar menuBar = new MenuBar();
  22. MenuItem menutem = new MenuItem();
  23. final JTextArea textArea = new JTextArea();
  24. setMenuBar(menuBar);
  25. Menu file = new Menu("File");
  26. menuBar.add(file);
  27. MenuItem newOption = new MenuItem("New");
  28. MenuItem open = new MenuItem("Open");
  29. MenuItem save = new MenuItem("Save");
  30. MenuItem close = new MenuItem("Exit");
  31. file.add(newOption);
  32. file.add(open);
  33. file.add(save);
  34. file.add(close);
  35. getContentPane().add(textArea);
  36.  
  37. newOption.addActionListener(new ActionListener(){
  38. public void actionPerformed(ActionEvent e){
  39. textArea.setText("");
  40. }
  41. });
  42.  
  43. open.addActionListener(new ActionListener(){
  44. public void actionPerformed(ActionEvent e){
  45. openFile();
  46. if (fileToOpen == JFileChooser.APPROVE_OPTION){
  47. textArea.setText("");
  48. try{
  49. Scanner scan = new Scanner(new FileReader(fileOpen.getSelectedFile().getPath()));
  50. while (scan.hasNext())
  51. textArea.append(scan.nextLine());
  52. } catch (Exception ex){
  53. System.out.println(ex.getMessage());
  54. }
  55. }
  56. }
  57. });
  58.  
  59. save.addActionListener(new ActionListener(){
  60. public void actionPerformed(ActionEvent e){
  61. saveFile();
  62. if (fileToSave == JFileChooser.APPROVE_OPTION){
  63. try {
  64. BufferedWriter out = new BufferedWriter(new FileWriter(fileSave.getSelectedFile().getPath()));
  65. out.write(textArea.getText());
  66. out.close();
  67. } catch (Exception ex) {
  68. System.out.println(ex.getMessage());
  69. }
  70. }
  71. }
  72. });
  73.  
  74. close.addActionListener(new ActionListener(){
  75. public void actionPerformed(ActionEvent e){
  76. System.exit(0);
  77. }
  78. });
  79. }
  80.  
  81. public void openFile(){
  82. JFileChooser open = new JFileChooser();
  83. int option = open.showOpenDialog(this);
  84. fileToOpen = option;
  85. fileOpen = open;
  86. }
  87.  
  88. public void saveFile(){
  89. JFileChooser save = new JFileChooser();
  90. int option = save.showOpenDialog(this);
  91. fileToSave = option;
  92. fileSave = save;
  93. }
  94. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

plz send me the full window notepad source code plzzzz email ID : [email protected]

first i admire to create the notepad
then i want to say where is the main method

sir plz send me windows notepad source code and windows note pad jar file plzsir my email id [email protected]

It is nice bt it opeARES ONLY OPEN & SAVE option. please, add the Search and Highlighting the searched text....

good site. it is very usefull to all. thanks.

but i need the complete notepad function.

Checkout the notepad i have coded @easylearnjava.wordpress.com

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.