Report
Report
1.INTRODUCTION:
Text Editor: - A text editor is a type of program used for editing plain text files. This project
"Text Editor" is software which can edit plain text. It is made using Java Swings and AWT.
In this project all the frames are designed in Swing. Swing is a set of classes that provides
more powerful and flexible GUI components than does the AWT. Swing provides the look
and feel of the modern Java GUI.
Swing did not exist in the early days of Java. Rather, it was a response to deficiencies present
in Java's original GUI subsystem: the Abstract Window Toolkit. The AWT defines a basic
set of controls, windows, and dialog boxes that support a usable, but limited graphical
interface.
1.1Objective:
The objective of this project is to design and implement a simple text editor with essential
features like creating new files, opening existing files, saving files, editing text, and
providing basic text formatting options using Java's Swing GUI and AWT libraries.
Swing: A GUI widget toolkit for Java, part of the Java Foundation Classes (JFC). It provides
more sophisticated components than AWT.
3.Features:
File Menu: File Menu contains the MenuItem within it like New,Open,Save,SaveAs,Exit
operations.
Multiple font colors - The editor provides 3 different font colors to make a text more
SSWP/IT/2024-25 Page 1 of 12
Advance Java Programming (22517) TextEditor
Fonts the editor provides all fonts that can be used to make a text more appealing.
WordWrap: Enables automatic line wrapping to fit within the window’s width.
keyboard shortcuts for increased productivity. The shortcuts allow users to perform
common tasks (like saving, opening, and creating new files) without needing to navigate
through the menu.
4.Implementation Details:
JFrame: The main window of the application, where all other components are placed.
JMenuBar: A menu bar containing "File" and "Edit" menus for various actions.
JMenuItem: Individual items within the menus for specific actions like "New," "Open,"
"Save," "Exit," etc.
JScrollPane: Adds scroll functionality to the JTextArea when the text exceeds the visible
area.
ActionListener: Used to capture and respond to user actions like menu clicks.
i] import javax.swing.*;
import java.awt.event.*;
import java.awt.FileDialog.*;
import java.awt.Font.*;
import javax.swing.undo.UndoManager;
import javax.swing.event.*;
import java.awt.Window.*;
public class NotepadProject implements ActionListener
SSWP/IT/2024-25 Page 2 of 12
Advance Java Programming (22517) TextEditor
{ JFrame window;
JTextArea text;
JScrollPane scroll;
boolean wordWrapaOn=false;
JMenuBar mb;
JMenu menuFile,Edit,Format,Color;
JMenuItem minew,miopen,misave,misaveas,miexit,
JMenuItem wordwrap,fontArial,fontTNR,fontCSMS,fontsize8,fontsize10,fontsize12,
fontsize14,fontsize 4;
JMenu font,fontSize;
JMenuItem icolor1,icolor2,icolor3;
JMenuItem iUndo,iRedo;
Fuction_File fi =new Fuction_File(this) ;
Function_Format fm=new Function_Format(this);
Function_Color fc=new Function_Color(this);
Function_Edit fe=new Function_Edit(this);
UndoManager um=new UndoManager();
public static void main(String arg[]){ new NotepadProject(); }
public NotepadProject(){
CreteWindow();
CreatTextArea();
CreateMenu();
CreateFileMenu();
CreateFormatMenu();
CreateColorMenu();
CreateEditMenu();
fm.selectedFont= "Arial";
fm.createFont(14);
fm.WordWrap();
fc.changeColor("black");
window.setVisible(true); }
public void CreteWindow(){
SSWP/IT/2024-25 Page 3 of 12
Advance Java Programming (22517) TextEditor
window=new JFrame("Notepad");
window.setSize(400,400);
window.setDefaultCloseOperation(window.EXIT_ON_CLOSE); }
public void CreatTextArea() {
text=new JTextArea();
text.getDocument().addUndoableEditListener(new UndoableEditListener(){ public
void undoableEditHappened(UndoableEditEvent ue)
{ um.addEdit(ue.getEdit()); } });
scroll=newJScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScro
llPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setBorder(BorderFactory.createEmptyBorder());
window.add(scroll); }
public void CreateMenu()
{ mb=new JMenuBar();
window.setJMenuBar(mb);
menuFile=new JMenu("File");
menuFile.setMnemonic('F');
mb.add(menuFile);
Edit=new JMenu("Edit");
Edit.setMnemonic('E');
mb.add(Edit);
Format=new JMenu("Format");
mb.add(Format);
Color=new JMenu("Color");
Color.setMnemonic('C');
mb.add(Color); }
public void CreateFileMenu(){ minew=new JMenuItem("New");
minew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_DOWN_MASK));
minew.addActionListener(this);
minew.setActionCommand("New");
menuFile.add(minew);
miopen=new JMenuItem("Open");
SSWP/IT/2024-25 Page 4 of 12
Advance Java Programming (22517) TextEditor
miopen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_DOWN_MASK));
miopen.addActionListener(this);
miopen.setActionCommand("Open");
menuFile.add(miopenmisave=new JMenuItem("Save");
misave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_DOWN_MASK));
misave.addActionListener(this);
misave.setActionCommand("Save");
menuFile.add(misave);
misaveas=new JMenuItem("SaveAs");
misaveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_D
OWN_MASK|InputEvent.SHIFT_DOWN_MASK));
misaveas.addActionListener(this);
misaveas.setActionCommand("SaveAs");
menuFile.add(misaveas);
miexit=new JMenuItem("Exit");
miexit.addActionListener(this);
miexit.setActionCommand("Exit");
menuFile.add(miexit);}
public void CreateFormatMenu(){
wordwrap=new JMenuItem("Word Wrap:Off");
wordwrap.setActionCommand("wordWrap");
wordwrap.addActionListener(this);
Format.add(wordwrap);
font =new JMenu("Font");
font.setActionCommand("Font");
font.addActionListener(this);
Format.add(font);
fontArial=new JMenuItem("Arial");
fontArial.addActionListener(this);
fontArial.setActionCommand("Arial");
font.add(fontArial);
SSWP/IT/2024-25 Page 5 of 12
Advance Java Programming (22517) TextEditor
SSWP/IT/2024-25 Page 6 of 12
Advance Java Programming (22517) TextEditor
SSWP/IT/2024-25 Page 7 of 12
Advance Java Programming (22517) TextEditor
SSWP/IT/2024-25 Page 8 of 12
Advance Java Programming (22517) TextEditor
note.window.setTitle(fileName); }
System.out.println("File address and file name"+FileAddress+fileName);
Try{ BufferedReader br=new BufferedReader(new FileReader(FileAddress +
fileName));
note.text.setText("");
String line=null;
while((line = br.readLine())!=null; {
note.text.append(line +"\n"); }
br.close(); }
catch(Exception e) {
System.out.println("File not opened"); }}
public void save(){
if(fileName==null){
SaveAs();}
else{
try{
FileWriter fw= new FileWriter(FileAddress +fileName);
fw.write(note.text.getText());
note.window.setTitle(fileName);
fw.close(); }
catch(Exception e){
System.out.println("Something Wrong");}}}
public void SaveAs(){
FileDialog fd=new FileDialog(note.window,"save",FileDialog.SAVE);
fd.setVisible(true);
if(fd.getFile()!=null){
fileName =fd.getFile();
FileAddress =fd.getDirectory();
note.window.setTitle(fileName); }
try{
FileWriter fw=new FileWriter(FileAddress + fileName);
fw.write(note.text.getText());
SSWP/IT/2024-25 Page 9 of 12
Advance Java Programming (22517) TextEditor
fw.close(); }
catch(Exception e) {
System.out.println("Something Wrong");}}
public void Exit(){
System.exit(0);}}
4.3:Output:
SSWP/IT/2024-25 Page 10 of
12
Advance Java Programming (22517) TextEditor
5.Conclusion:
This project successfully implements a basic text editor with essential file-handling
features, a Word-wrap toggle option, and convenient keyboard shortcuts. The use of Java
Swing and AWT provides a simple yet effective graphical interface. The addition of
shortcuts improves the productivity and usability of the editor, allowing users to perform
common tasks efficiently.
SSWP/IT/2024-25 Page 11 of
12
Advance Java Programming (22517) TextEditor
6.References:
SSWP/IT/2024-25 Page 12 of
12