Skip to content

Commit 916ed26

Browse files
add: text editor project in java
1 parent b10d69b commit 916ed26

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

textEditor.java

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
import java.io.*;
4+
import java.util.*;
5+
//Texteditor class starts here
6+
class Texteditor extends Frame implements ActionListener
7+
{
8+
TextArea ta=new TextArea();
9+
int i,len1,len,pos1;
10+
String str="",s3="",s2="",s4="",s32="",s6="",s7="",s8="",s9="";
11+
String months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
12+
CheckboxMenuItem chkb=new CheckboxMenuItem("Word Wrap");
13+
public Texteditor()
14+
{
15+
MenuBar mb=new MenuBar();
16+
setLayout(new BorderLayout());
17+
add("Center",ta);
18+
setMenuBar(mb);
19+
Menu m1=new Menu("File");
20+
Menu m2=new Menu("Edit");
21+
Menu m3=new Menu("Tools");
22+
Menu m4=new Menu("Help");
23+
mb.add(m1);
24+
mb.add(m2);
25+
mb.add(m3);
26+
mb.add(m4);
27+
MenuItem mi1[]={
28+
new MenuItem("New"),new MenuItem("Open"),new MenuItem("Save")
29+
,new MenuItem("Save As"),new MenuItem("Page Setup")
30+
,new MenuItem("Print"),new MenuItem("Exit")
31+
};
32+
MenuItem mi2[]={new MenuItem("Delete"),new MenuItem("Cut"),
33+
new MenuItem("Copy"),new MenuItem("Paste"),new MenuItem("Find"),
34+
new MenuItem("Find Next"),new MenuItem("Replace"),
35+
new MenuItem("Go To"),new MenuItem("Select All"),
36+
new MenuItem("Time Stamp")};
37+
MenuItem mi3[]={new MenuItem("Choose Font"),new MenuItem("Compile"),
38+
new MenuItem("Run")};
39+
MenuItem mi4[]={new MenuItem("Help Topics"),
40+
new MenuItem("About Texteditor")};
41+
for(int i=0;i<len1;i++ )="" <br=""> {
42+
m1.add(mi1[i]);
43+
mi1[i].addActionListener(this);
44+
}
45+
for(int i=0;i<len1;i++) {="" <br=""> m2.add(mi2[i]);
46+
mi2[i].addActionListener(this);
47+
}
48+
m3.add(chkb);
49+
chkb.addActionListener(this);
50+
for(int i=0;i<len1;i++ )="" <br=""> {
51+
m3.add(mi3[i]);
52+
mi3[i].addActionListener(this);
53+
}
54+
for(int i=0;i<len1;i++) <br=""> {
55+
m4.add(mi4[i]);
56+
mi4[i].addActionListener(this);
57+
}
58+
MyWindowsAdapter mw=new MyWindowsAdapter(this);
59+
addWindowListener(mw);
60+
setSize(500,500);
61+
setTitle("untitled notepad");
62+
setVisible(true);
63+
}
64+
public void actionPerformed(ActionEvent ae)
65+
{
66+
String arg=(String)ae.getActionCommand();
67+
if(arg.equals("New"))
68+
{ dispose();
69+
Texteditor t11=new Texteditor();
70+
t11.setSize(500,500);
71+
t11.setVisible(true);
72+
}
73+
try {
74+
if(arg.equals("Open"))
75+
{
76+
FileDialog fd1=new FileDialog(this,"Select File",FileDialog.LOAD);
77+
fd1.setVisible(true);
78+
String s4="";
79+
s2=fd1.getFile();
80+
s3=fd1.getDirectory();
81+
s32=s3+s2;
82+
File f=new File(s32);
83+
FileInputStream fii=new FileInputStream(f);
84+
len=(int)f.length();
85+
for(int j=0;j<len1;j++ )="" <br=""> {
86+
char s5=(char)fii.read();
87+
s4=s4 + s5;
88+
}
89+
ta.setText(s4);
90+
}
91+
}
92+
catch(IOException e)
93+
{
94+
}
95+
try
96+
{
97+
if(arg.equals("Save As"))
98+
{
99+
FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
100+
dialog1.setVisible(true);
101+
s7=dialog1.getDirectory();
102+
s8=dialog1.getFile();
103+
s9=s7+s8+".txt";
104+
s6=ta.getText();
105+
len1=s6.length();
106+
byte buf[]=s6.getBytes();
107+
File f1=new File(s9);
108+
FileOutputStream fobj1=new FileOutputStream(f1);
109+
for(int k=0;k<len1;k++) <br=""> {
110+
fobj1.write(buf[k]);
111+
}
112+
fobj1.close();
113+
}
114+
this.setTitle(s8 +" Texteditor File");
115+
}
116+
catch(IOException e){}
117+
if(arg.equals("Exit"))
118+
{
119+
System.exit(0);
120+
}
121+
if(arg.equals("Cut"))
122+
{
123+
str=ta.getSelectedText();
124+
i=ta.getText().indexOf(str);
125+
ta.replaceRange(" ",i,i+str.length());
126+
}
127+
if(arg.equals("Copy"))
128+
{
129+
str=ta.getSelectedText();
130+
}
131+
if(arg.equals("Paste"))
132+
{
133+
pos1=ta.getCaretPosition();
134+
ta.insert(str,pos1);
135+
}
136+
if(arg.equals("Delete"))
137+
{
138+
String msg=ta.getSelectedText();
139+
i=ta.getText().indexOf(msg);
140+
ta.replaceRange(" ",i,i+msg.length());
141+
msg="";
142+
}
143+
if(arg.equals("Select All"))
144+
{
145+
String strText=ta.getText();
146+
int strLen=strText.length();
147+
ta.select(0,strLen);
148+
}
149+
if(arg.equals("Time Stamp"))
150+
{
151+
GregorianCalendar gcalendar=new GregorianCalendar();
152+
String h=String.valueOf(gcalendar.get(Calendar.HOUR));
153+
String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
154+
String s=String.valueOf(gcalendar.get(Calendar.SECOND));
155+
String date=String.valueOf(gcalendar.get(Calendar.DATE));
156+
String mon=months[gcalendar.get(Calendar.MONTH)];
157+
String year=String.valueOf(gcalendar.get(Calendar.YEAR));
158+
String hms="Time"+" - "+h+":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year+" ";
159+
int loc=ta.getCaretPosition();
160+
ta.insert(hms,loc);
161+
}
162+
if(arg.equals("About Texteditor"))
163+
{
164+
AboutDialog d1=new AboutDialog(this,"About Texteditor");
165+
d1.setVisible(true);
166+
setSize(500,500);
167+
}
168+
}//Action pereformed end
169+
public class MyWindowsAdapter extends WindowAdapter
170+
{
171+
Texteditor tt;
172+
public MyWindowsAdapter(Texteditor ttt)
173+
{
174+
tt=ttt;
175+
}
176+
public void windowClosing(WindowEvent we)
177+
{
178+
tt.dispose();
179+
}
180+
}//Inner class winadapter end....
181+
}//End of Texteditor class
182+
public class balls
183+
{ public static void main(String args[])
184+
{
185+
Texteditor to=new Texteditor();
186+
}
187+
}
188+
class AboutDialog extends Dialog implements ActionListener
189+
{
190+
AboutDialog(Frame parent,String title)
191+
{
192+
super(parent,title,false);
193+
this.setResizable(false);
194+
setLayout(new FlowLayout(FlowLayout.LEFT));
195+
setSize(500,300);
196+
}
197+
public void actionPerformed(ActionEvent ae)
198+
{
199+
dispose();
200+
}
201+
}

0 commit comments

Comments
 (0)