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
e-commerce management system in java Introduction The e-commerce management system is a GUI-based desktop application designed using Java swing in Netbean IDE. …
time table generator in java introduction The Time Table Generator is a Java utility that helps educational institutions automatically create class schedules …
crime record management system in java introduction The Crime Record Management System is a secure and systematic way of maintaining criminal and …
car rental system in java(GUI swing) introduction The Car Rental System is a Java application tailored for vehicle rental agencies. It allows …
food delivery management system in java introduction This Food Delivery Management System helps restaurants manage customer orders, menus, deliveries, and billing using …
online course registration in java introduction The Online Course Registration System allows students to enroll in courses using a Java application with …