Text Editor in Java with Source Code

Today, I am going to make a Text Editor program using our Java. Yeah, Like notepad. I want to add some basic functionalities like Opening a file, Saving a file and option to change the font size. It will only take less than 100 lines of code to do this (Excluding User Interface).
I started with the toolbar javax.swing.JToolBar and added 4 buttons. Button with simple label logo and text for open,save and font size changing. The display area is simply a JTextArea with monotype font.

Opening a Text File

It is pretty straightforward to use JFileChooser to select a file. When I started thinking about opening a file, I wanted a File Chooser that filter and display only text files.Thanks to FileNameExtensionFilter from javax.swing.filechooser.FileNameExtensionFilter. I am able to display only text files on the JFileChooser using the code
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
When the user selects a file and click open, The Scanner from java.util.Scanner is used to read the file.
Scanner scn = new Scanner(file);  
String buffer = "";  
while (scn.hasNext()) {  
   buffer += scn.nextLine() + "\n";  
The “file” is nothing but the file taken from the JFileChooser and the while loop will continue until the end of file. “display” is the name of JTextArea, our display.

Saving a Text File

A global file variable called “currentEditingFile” is maintained to know whether we are working on a new, unsaved data or on an opened file. If “currentEditingFile” is null we have a new uncreated file. So the saving method will look to “currentEditingFile” and if it is null a JFileChooser will be opened to select the location for the new file. After selecting the directory, file name is taken from JOptionPane input dialog.
The file is then written onto the file Using PrintWriter from java.io.PrintWriter.

Changing Font Size

A global variable with default font size as 14 is maintained and increased or decreased depending upon the button pressed.
display.setFont(new java.awt.Font("Monospaced", 0, ++fontSize));  //Increase font size
display.setFont(new java.awt.Font("Monospaced", 0, --fontSize)); //Decrease font size

Extra Works

It is not acceptable to exit the program without any notice when the user clicks on the Close button. In order to add a save confirmation, i made an override on the Window Closing operating and added a confirmation message. It is done from the constructor itself
this.addWindowListener(new WindowAdapter() {  
   public void windowClosing(WindowEvent e) {  
	 super.windowClosing(e); //To change body of generated methods, choose Tools | Templates.  
	 int ans = JOptionPane.showConfirmDialog(rootPane, "Save Changes ?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);  
	 if (ans == JOptionPane.YES_OPTION) {  
	   //Write Changes 

So that’s it. Download the program / source code from below buttons. If this was helpful please like me on facebook and keep visiting for more cool programs and sources.

Recommended Read

Download Source Code Download Application 


Detachable Tool Panel
Font size control
File selector window
Save complete dialog
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.