Text Editor Using Java Swing With Source Code

Introduction:
In this project, we will show how to develop Text Editor using Java Swing library. This Text Editor allows you to save and open a text file using a graphical user interface (GUI) application. This system is develop with the Java Swing library which gives a set of components that can be use to build a Graphical user interface(GUI). This application has an easy and simple design with buttons to save and open a Text File. So grab a seat, pour a cup of coffee and start using Text Editor application.
Explanation:
This Text Editor is a Graphical user interface(GUI) system that allows user to easily create and save the text file or to open and modify or read the text file using buttons. Users may very quickly edit the text using the system simple design. The Text editor has simple design with buttons and TextArea. The user can write a text in TextArea and can save the file on the device or user can open a text file available on the device and can modify or read that file.
The users may save the text file using save button or open a text file using open button. The user can write their content in the Text Area and save the file on device using save button as well as user can open any file available on device. Further user can change the text and can again save the file.
The Java Swing library which gives a flexible set of components is used to build the Text Editor. The application makes use of Swing components such as JTextArea, JButton and JPanel among many other Swing components. Button event is handled by the ActionListener interface and button events are defined using ActionListener’s actionPerformed() method.
Methods used in the project:
actionPerformed():
The action or events of the buttons are handled using this method. Buttons in the system to save a file and to open a file is handled using this method. This method describe how the components should behave after the button is click.
Source Code:
Get Discount on Top Educational Courses
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Text_Editor extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JButton openButton, saveButton;
public Text_Editor() {
super("Text Editor");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.PAGE_START);
openButton = new JButton("Open");
openButton.addActionListener(this);
buttonPanel.add(openButton);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
buttonPanel.add(saveButton);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openButton) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt"));
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
FileReader reader = new FileReader(fileChooser.getSelectedFile());
BufferedReader br = new BufferedReader(reader);
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
textArea.setText(sb.toString());
br.close();
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == saveButton) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt"));
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
FileWriter writer = new FileWriter(fileChooser.getSelectedFile());
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Text_Editor();
}
}
Output:


Find More Projects
URL Shortener Using Python Django Introduction: Long URLs can be shortened into short, shareable links with the help of the URL Shortener …
User Authentication System Using Python Django Introduction: The implementation of safe and adaptable user authentication in Django is the main goal of …
The E-Learning System using Java with a Graphical User Interface (GUI) Introduction The E-Learning System is developed using Java (with a Graphical …
Weather App Using Python Django Introduction: When a user enters the name of a city, the Weather App retrieves current weather information. …
Quiz App Using Python Django Introduction: Users can take quizzes in a variety of subjects, see their results, and monitor their progress …
resume screener in python using python introduction The hiring process often begins with reviewing numerous resumes to filter out the most suitable …