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.
import javax.swing.JFrame;
public class Main {
public static void main
(String args
[]){
frame.setTitle("Notepad");
frame.setVisible(true);
frame.setSize(1280, 720);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
}
}
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.
int fileToOpen;
int fileToSave;
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.
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{
Scanner scan
= new Scanner
(new FileReader(fileOpen.
getSelectedFile().
getPath()));
while (scan.hasNext())
textArea.append(scan.nextLine());
System.
out.
println(ex.
getMessage());
}
}
}
});
saveFile();
try {
out.write(textArea.getText());
out.close();
System.
out.
println(ex.
getMessage());
}
}
}
});
}
});
}
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...
public void openFile(){
int option = open.showOpenDialog(this);
fileToOpen = option;
fileOpen = open;
}
public void saveFile(){
int option = save.showOpenDialog(this);
fileToSave = option;
fileSave = save;
}
Project Complete!
That's it! Below is the full code and download to the files. Thanks for reading!
Main:
import javax.swing.JFrame;
public class Main {
public static void main
(String args
[]){
frame.setTitle("Notepad");
frame.setVisible(true);
frame.setSize(1280, 720);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
}
}
Grid:
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;
public class Grid
extends JFrame{
int fileToOpen;
int fileToSave;
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{
Scanner scan
= new Scanner
(new FileReader(fileOpen.
getSelectedFile().
getPath()));
while (scan.hasNext())
textArea.append(scan.nextLine());
System.
out.
println(ex.
getMessage());
}
}
}
});
saveFile();
try {
out.write(textArea.getText());
out.close();
System.
out.
println(ex.
getMessage());
}
}
}
});
}
});
}
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.