How to Create a Notepad Project in Java
Submitted by Yorkiebar on Tuesday, September 24, 2013 - 08:46.
Language
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.
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.
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.
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...
Project Complete!
That's it! Below is the full code and download to the files. Thanks for reading!
Main:
Grid:
- import javax.swing.JFrame;
- public class Main {
- frame.setTitle("Notepad");
- frame.setVisible(true);
- frame.setSize(1280, 720);
- frame.setResizable(true);
- frame.setLocationRelativeTo(null);
- }
- }
- int fileToOpen;
- int fileToSave;
- JFileChooser fileOpen;
- JFileChooser fileSave;
- Grid(){
- setMenuBar(menuBar);
- menuBar.add(file);
- file.add(newOption);
- file.add(open);
- file.add(save);
- file.add(close);
- getContentPane().add(textArea);
- textArea.setText("");
- }
- });
- openFile();
- textArea.setText("");
- try{
- while (scan.hasNext())
- textArea.append(scan.nextLine());
- }
- }
- }
- });
- saveFile();
- try {
- out.write(textArea.getText());
- out.close();
- }
- }
- }
- });
- }
- });
- }
- public void openFile(){
- int option = open.showOpenDialog(this);
- fileToOpen = option;
- fileOpen = open;
- }
- public void saveFile(){
- int option = save.showOpenDialog(this);
- fileToSave = option;
- fileSave = save;
- }
- import javax.swing.JFrame;
- public class Main {
- frame.setTitle("Notepad");
- frame.setVisible(true);
- frame.setSize(1280, 720);
- frame.setResizable(true);
- frame.setLocationRelativeTo(null);
- }
- }
- import java.awt.Menu;
- import java.awt.MenuBar;
- import java.awt.MenuItem;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.Scanner;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JTextArea;
- int fileToOpen;
- int fileToSave;
- JFileChooser fileOpen;
- JFileChooser fileSave;
- Grid(){
- setMenuBar(menuBar);
- menuBar.add(file);
- file.add(newOption);
- file.add(open);
- file.add(save);
- file.add(close);
- getContentPane().add(textArea);
- textArea.setText("");
- }
- });
- openFile();
- textArea.setText("");
- try{
- while (scan.hasNext())
- textArea.append(scan.nextLine());
- }
- }
- }
- });
- saveFile();
- try {
- out.write(textArea.getText());
- out.close();
- }
- }
- }
- });
- }
- });
- }
- public void openFile(){
- int option = open.showOpenDialog(this);
- fileToOpen = option;
- fileOpen = open;
- }
- public void saveFile(){
- int option = save.showOpenDialog(this);
- fileToSave = option;
- fileSave = save;
- }
- }
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
windows notepad source code and windows note pad jar file
sir plz send me windows notepad source code and windows note pad jar file plzsir my email id rajechaudhari2@gmail.com
first i admire to create the
first i admire to create the notepad
then i want to say where is the main method
Notepad source code
plz send me the full window notepad source code plzzzz email ID : imranawan1991@gmail.com
Add new comment
- Add new comment
- 5729 views