Learn How to Create a Text Editor with JAVA
Table of content
|
The following example illustrates creating aText Editor in Java by
using the simple Java classes and some GUI interface built for the user.In
this Text Editor each User has its own space,somehow we aren’t going to
use any Database but in order to save Username and Password we use the txt
file.
The software that You need to install is Eclipse.
No external library required to build this Text Editor
application. you can
Download the zip file of the project but before downloading you need to learn how
its work.
Let’s start it:
Chat Box in JAVA
Get the CODE⬅️.
Step1:Create the new Project.
Step2:In this project we create the four Java Classes named as:
1.Login.java
2.Register.java
3.FileBrowser.java
4.Editor.java
Step3:After creating all the classes we are going to create GUI windows for all those class.Here as I take the JAVA classes,you can use the window builder tool and create all the classes as JFrames the name should be same.
Graphical Interface
of all the classes:-
■Login.java
■ Register.java
■FileBrowser.java
■Editor.java
Working of the Project:
● In the First Step you need to register yourself by default it opens the
Login panel where you just click the Register button and choose the
Username and Password that are stored in txt file.
As
Shown in the image password is stored in encryption form.
● And a folder is created with the name of the user.
●When the user is login the file browser window is open,here user can
create new file.
Python Mini Projects
Get the CODE⬅️.
● And this new file is saved in the folder of that user name as shown in
the below image.
● All the files created by user is appear as a list,user can select
the file and click the open button.
● The new panel is open where user can write the text into file,save it or
close it.
Some important coding part
from this project.Download the Full Project.After downloading it you just open the folder into
eclipse
Working of login Button
if(e.getSource()== login) {
try
{
//getting user information
BufferedReader input
= new
BufferedReader(new
FileReader("passwords.txt"));
String pass
= null;
String line
= input.readLine();
while(line
!= null) {
StringTokenizer st
= new
StringTokenizer(line);
if(userTF.getText().equals(st.nextToken()))
pass
= st.nextToken();
line
= input.readLine();
}
input.close();
//converting password from encryption form for login
MessageDigest md
=MessageDigest.getInstance("SHA-256");
md.update(new
String(passTF.getPassword()).getBytes());
byte
byteData[] = md.digest();
StringBuffer sb
= new
StringBuffer();
for(int
i=0; i
< byteData.length; i++)
sb.append(Integer.toString((byteData[i] & 0xFF) +
0x100,16).substring(1));
if(pass.equals(sb.toString()))
add(new
FileBrowser(userTF.getText()),"fb");
cl.show(this,"fb"
);
} catch
(FileNotFoundException e1) {
// TODO
Auto-generated catch block
e1.printStackTrace();
|
Register the user
// register the user
if(e.getSource() == register
&& passTF.getPassword().length>0 &&
userTF.getText().length()>0) {
String pass
= new
String(passTF.getPassword());
String confirm
= new
String(passC.getPassword());
//check the password again
if(pass.equals(confirm)) {
try
{
BufferedReader input
= new
BufferedReader(new
FileReader("passwords.txt"));
String line
= input.readLine();
while(line
!= null) {
StringTokenizer st
= new
StringTokenizer(line);
//username must be unique
if(userTF.getText().equals(st.nextToken())) {
System.out.println("User already
exists");
return;
}
line
= input.readLine();
}
input.close();
MessageDigest md
=MessageDigest.getInstance("SHA-
256");
md.update(pass.getBytes());
byte
byteData[] = md.digest();
StringBuffer sb
= new
StringBuffer();
for(int
i=0; i
< byteData.length; i++)
sb.append(Integer.toString((byteData[i] &
0xFF) + 0x100,16).substring(1));
BufferedWriter output
= new
BufferedWriter(new
FileWriter("passwords.txt",true));
output.write(userTF.getText() + " "
+ sb.toString()
+ "\n");
output.close();
Login login
=(Login) getParent();
login.cl.show(login, "login");
} catch
(FileNotFoundException e1) {
// TODO
Auto-generated catch block
e1.printStackTrace();
|
Browse the Files:
if(e.getSource()==open) {
//open the selected file
login.add(new
Editor(directory.getName() + "\\" +
bg.getSelection().getActionCommand()),"editor");
login.cl.show(login, "editor");
}
if(e.getSource()==newFile) {
//creating new file with .txt extension
String file
= directory.getName()+"\\"+newFileTF.getText()+
".txt";
if(newFileTF.getText().length() > 0 && !(new
File(file).exists())) {
login.add(new
Editor(file),"editor");
login.cl.show(login, "editor");
}
|
Save the text written in the Text Editor
try
{
//save the content
FileWriter out
= new
FileWriter(file);
out.write(text.getText());
out.close();
if(e.getSource()==savec) {
Login login
=(Login) getParent();
login.cl.show(login, "fb");
}
} catch
(IOException el) {
// TODO
Auto-generated catch block
el.printStackTrace();
}
|
Download the full Project in zip file.
0 Comments