0% found this document useful (0 votes)
8 views12 pages

Report

report

Uploaded by

arpitajagdale1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Report

report

Uploaded by

arpitajagdale1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Advance Java Programming (22517) TextEditor

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.

2. Tools and Technologies Used:

Java: Programming language used to build the application.

Swing: A GUI widget toolkit for Java, part of the Java Foundation Classes (JFC). It provides
more sophisticated components than AWT.

AWT (Abstract Window Toolkit): Java's original platform-dependent windowing, graphics,


and user interface widget toolkit. It is used alongside Swing for event handling and some
components.

3.Features:

Our text editor provides basic features of an editor.

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

interactive and attractive.

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.

Edit Menu: The editor provides Undo,Redo operations

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:

4.1. Components Used:

JFrame: The main window of the application, where all other components are placed.

JTextArea: A multiline area to display and edit the text.

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.

FileDialog: A dialog for selecting files when opening or saving.

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.

KeyStroke: Used to implement keyboard shortcuts.

4.2. Code Overview:

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

fontCSMS=new JMenuItem("Comic Sans Ms");


fontCSMS.addActionListener(this);
fontCSMS.setActionCommand("Comic Sans Ms");
font.add(fontCSMS);
fontTNR=new JMenuItem("Times New Roman");
fontTNR.addActionListener(this);
fontTNR.setActionCommand("Times New Roman");
font.add(fontTNR);
fontSize=new JMenu("FontSize");
fontSize.setActionCommand("FontSize");
fontSize.addActionListener(this);
Format.add(fontSize);
fontsize8=new JMenuItem("8");
fontsize8.setActionCommand("Size8");
fontsize8.addActionListener(this);
fontSize.add(fontsize8);
fontsize10=new JMenuItem("10");
fontsize10.setActionCommand("Size10");
fontsize10.addActionListener(this);
fontSize.add(fontsize10);
fontsize12=new JMenuItem("12");
fontsize12.setActionCommand("Size12");
fontsize12.addActionListener(this);
fontSize.add(fontsize12);
fontsize14=new JMenuItem("14");
fontsize14.setActionCommand("Size14");
fontsize14.addActionListener(this);
fontSize.add(fontsize14);
fontsize24=new JMenuItem("24");
fontsize24.setActionCommand("Size24");
fontsize24.addActionListener(this);
fontSize.add(fontsize24) }

SSWP/IT/2024-25 Page 6 of 12
Advance Java Programming (22517) TextEditor

public void CreateColorMenu() {


icolor1=new JMenuItem("White");
icolor1.addActionListener(this);
icolor1.setActionCommand("White");
Color.add(icolor1); icolor2=new JMenuItem("Black");
icolor2.addActionListener(this);
icolor2.setActionCommand("Black");
Color.add(icolor2); icolor3=new JMenuItem("Blue");
icolor3.addActionListener(this);
icolor3.setActionCommand("Blue");
Color.add(icolor3); }
public void CreateEditMenu() {
iUndo=new JMenuItem("Undo");
iUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
InputEvent.CTRL_DOWN_MASK));
iUndo.addActionListener(this);
iUndo.setActionCommand("Undo");
Edit.add(iUndo);
iRedo=new JMenuItem("Redo");
iRedo.addActionListener(this);
iRedo.setActionCommand("Redo");
Edit.add(iRedo)}
public void actionPerformed(ActionEvent ae){
String command=ae.getActionCommand();
switch(command{
case "New": fi.newFile(); break;
case "Open": fi.Open(); break;
case "SaveAs": fi.SaveAs();break;
case "Save":fi.save(); break;
case "Exit":fi.Exit(); break;
case "wordWrap":fm.WordWrap(); break;
case "Undo":fe.undo();break;

SSWP/IT/2024-25 Page 7 of 12
Advance Java Programming (22517) TextEditor

case "Redo":fe.redo(); break;


case "Arial": fm.setFont(command); break;
case "Comic Sans Ms":fm.setFont(command);break;
case "Times New Roman": fm.setFont(command); break;
case "Size8": fm.createFont(8);break;
case "Size10":fm.createFont(10);break;
case "Size12":fm.createFont(12);break;
case "Size14":fm.createFont(14);break;
case "Size24":fm.createFont(24);break;
case"White":fc.changeColor(command);break;
case"Black":fc.changeColor(command); break;
case"Blue":fc.changeColor(command);break;
} }}
ii] import javax. swing.*;
import java.awt. FileDialog;
import java.awt.*;
import java.io.*;
public class Fuction_File
{ NotepadProject note;
String fileName;
String FileAddress;
public Fuction_File( NotepadProject note) {
this.note=note;}
public void newFile() {
note.text.setText("");
note.window.setTitle("New"); }
public void Open() {
FileDialog fd = new FileDialog(note.window, "Open", FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile()!=null){
fileName=fd.getFile();
FileAddress=fd.getDirectory();

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:

1. Java SE Documentation: https://docs.oracle.com/javase/8/docs/api/


2. Javatpoint:https://www.javatpoint.com/awt-and-swing-in-java
3. AWT Event Handling: https://docs.oracle.com/javase/8/docs/technotes/guides/awt/

SSWP/IT/2024-25 Page 12 of
12

You might also like