0% found this document useful (0 votes)
14 views

Texted It

Uploaded by

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

Texted It

Uploaded by

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

Text editor:

Text editor Intro


The project involves building a text editor in Java, using Java Swing for creating
a graphical user interface.
The text editor will have a window (frame) with a menu bar, consisting of two menus
- file menu and edit menu.
The file menu will have features like new window, save file and open file.
The edit menu will have five functions - Cut, Copy, Paste, select All and Close
button.
The text area inside the window will be used for writing and editing the text.
The project demo shows a simple text editor created using Java Swing, with two
menus (file and edit) and multiple features within each menu.
Users can write and edit text inside the text area.
you will be creating a basic UI for a text editor project in Java using Java Swing
components.
In the previous video, we discussed the design for the text editor window
application.
We are creating a new project in Intellij for the text editor and naming it text
data".
We select Java as the language and Intellij as the build system, and choose Open
JDK 8 as the preferred JDK version.
we refactor the main class to be called "text editor".
we create a constructor for the text editor class and call it in the main function
to create an instance of the text editor application.
To create an application window, we use the Jframe class from the Java Swing
package.
we declare a data member "frame" and initialize it as a new Jframe with dimensions
400x400.
we make the frame visible and set the layout.
The resulting application window is a simple window with dimensions 400x400.
Text editor Adding Action Listeners
The video discusses Action Listener classes and action events. The current project
has a basic UI with menus and a text area, but clicking on the menu items does not
result in any action. Action events occur when a button is clicked or a checkbox is
checked/unchecked, or a menu item is selected. To listen to these action events, we
need to implement the Action Listener class and override its action performed
method. In the action performed method, we define the action to be taken when a
specific action event occurs, based on the event source. To implement this in our
text editor class, we need to implement the action performed method from the Action
Listener interface and override it.
Text editor Adding File Menu Functionalities
The video discusses how to implement file menu functionalities.
The file menu has three features: new window, open file, and save file.
The open file feature will be built first, followed by the save file feature.
The structure of open file and save file is similar, so they will be built
together.
To open a file, a file chooser is triggered by clicking on the open menu item.
The user must select the file path and file to open and click on the open button.
The text contained in the file will be copied and pasted into the text area.
When the open button is clicked, an action event is triggered.
The action performed method listens to the action event.
The JFileChooser class is used to open the file chooser.
When the save button is clicked, the text inside the text area is saved to the
file.
The first step in the open file function is to open the file chooser.
he video focuses on implementing file menu functionalities, specifically the save
file functionality.
The save file functionality is similar to the open file functionality in terms of
selecting the file path and name using a file picker.
The difference is that instead of copying and pasting the text to the text area,
the save file functionality gets the text from the text area and saves it to a new
file.
To implement the save file functionality, a new "if" statement is created to check
if the action event source is the save file functionality.
A file chooser is initialized to select the file path and name using the show save
dialog method, which displays a save button as the approve option.
A new file is created with the chosen directory path and file name using the File
object.
The text from the text area is saved to the new file.
Text editor Final touch
This video, talks about adding finishing touches to the application's UI and
discusses the need for scroll bars to make the UI more user-friendly.
To add scroll bars, the first step is to add horizontal and vertical scroll bars.
The structure of the application's UI involves a J frame, menu bar, and text area.
To make the text area scrollable, it needs to be wrapped inside a scroll pane class
inside Java Swing. The scroll pane makes everything inside it scrollable, and it
needs to be contained inside a panel.
The new structure of the UI involves a panel that contains the scroll pane and the
text area. Borders can also be added inside the panel to make it more visually
appealing.
In the constructor, the text area needs to be added to the panel, and the panel
needs to be added to the scroll pane. The scroll pane needs to be made scrollable
only when the text goes beyond the borders of the application.
The code for adding scroll bars involves creating a scroll pane with vertical and
horizontal scroll bars as needed and adding the scroll pane to the panel.

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class TextEditor implements ActionListener{


JFrame frame;
JMenuBar menuBar;

JMenu file,edit;

JMenuItem newFile,openFile,saveFile;
JMenuItem cut,copy,paste,selectAll,close;

JTextArea textArea;
TextEditor(){
//frame for the entire text editor
frame = new JFrame();

//initialize menubar
menuBar = new JMenuBar();

//text area for the project


textArea=new JTextArea();

//initialize the menus file and edit

file =new JMenu("File");


edit =new JMenu("Edit");

//initialize the menu items for the file


newFile = new JMenuItem("New Window");
openFile=new JMenuItem("Open File");
saveFile=new JMenuItem("Save File");

//add ActionListener to all these items


//so when clicked on this an actionEvent occurs and some action can be
preformed
newFile.addActionListener(this);
openFile.addActionListener(this);
saveFile.addActionListener(this);

//add the items to the menu file


file.add(newFile);
file.add(openFile);
file.add(saveFile);

//initialize the menu items for the edit


cut = new JMenuItem("Cut");
paste=new JMenuItem("Paste");
copy = new JMenuItem("Copy");
selectAll=new JMenuItem("Select All");
close=new JMenuItem("Close");

//add ActionListener to all these items


//so when clicked on this an actionEvent occurs and some action can be
preformed
cut.addActionListener(this);
paste.addActionListener(this);
copy.addActionListener(this);
selectAll.addActionListener(this);
close.addActionListener(this);

//add the items to the menu file


edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);
edit.add(close);

//set menu to menubar


menuBar.add(file);
menuBar.add(edit);

//set menubar to frame


frame.setJMenuBar(menuBar);

//create the panel and set its border and layout


JPanel panel=new JPanel();
panel.setBorder(new EmptyBorder(5,5,5,5));
panel.setLayout(new BorderLayout(0,0));

//add text area to panel


panel.add(textArea,BorderLayout.CENTER);
//create a scroll pane with vertical and horizontal scrollbar
JScrollPane scrollPane=new
JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTA
L_SCROLLBAR_AS_NEEDED);
//add scroll pane to panel
panel.add(scrollPane);

//add panel to frame

frame.add(panel);
//set dimensions and layout of the frame
frame.setBounds(250,350,400,400);
frame.setTitle("TEXT EDITOR");
frame.setVisible(true);
frame.setLayout(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent){
if(actionEvent.getSource()==cut){
//perform cut operation
textArea.cut();
}
if(actionEvent.getSource()==copy){
//perform copy operation
textArea.copy();
}
if(actionEvent.getSource()==paste){
//perform paste operation
textArea.paste();
}
if(actionEvent.getSource()==selectAll){
//perform selectAll operation
textArea.selectAll();
}
if(actionEvent.getSource()==close){
//perform close editor operation
System.exit(0);
}
if(actionEvent.getSource()==openFile){
//perform close editor operation
JFileChooser fileChooser=new JFileChooser("C:");
int chooseOption = fileChooser.showOpenDialog(null);
//if we have clicked on open the file button
if(chooseOption==JFileChooser.APPROVE_OPTION){
//getting selected file

File file=fileChooser.getSelectedFile();
//get path of selected file
String filePath = file.getPath();
try{
//initalize file reader
FileReader fileReader=new FileReader(filePath);
//initialize buffer rader
BufferedReader bufferedReader=new BufferedReader(fileReader);
String intermediate="",output="";

//Read contents of file line by line take it


//into the intermediate and paste to output
while((intermediate=bufferedReader.readLine())!=null){
output+=intermediate+"\n";
}
//set output string to text area
textArea.setText(output);
}
catch (IOException fileNotFoundException){
fileNotFoundException.printStackTrace();

}
}
}
if(actionEvent.getSource()==saveFile) {
//perform close editor operation
JFileChooser fileChooser = new JFileChooser("C:");
//get choose option from file chooser
int chooseOption = fileChooser.showSaveDialog(null);
//if we have clicked on open the file button
if (chooseOption == JFileChooser.APPROVE_OPTION) {
//getting selected file

File file = new


File(fileChooser.getSelectedFile().getAbsolutePath() + ".txt");
//save the content to the new File
try {
//initialize file writer
FileWriter fileWriter = new FileWriter(file);
//initialize buffer writer
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

//write the content of the file to the new file


textArea.write((bufferedWriter));
bufferedWriter.close();
} catch (IOException ioException) {
ioException.printStackTrace();

}
}
}
if(actionEvent.getSource()==newFile){
TextEditor newTextEditor=new TextEditor();
}
}

public static void main(String[] args) {


TextEditor textEditor=new TextEditor();
}
}
explain eacch lines

2 / 2

ChatGPT
Sure, let's go through the code and explain each part:

java
Copy code
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
This part imports necessary classes from the Java Swing package for creating the
GUI, as well as classes for handling file operations and events.

java
Copy code
public class TextEditor implements ActionListener{
This declares a class named TextEditor which implements the ActionListener
interface. This means the TextEditor class will listen for and handle actions
triggered by GUI components.

java
Copy code
JFrame frame;
JMenuBar menuBar;

JMenu file,edit;

JMenuItem newFile,openFile,saveFile;
JMenuItem cut,copy,paste,selectAll,close;

JTextArea textArea;
Here, various Swing components are declared. frame is the main window of the
application. menuBar is the menu bar that holds menus. file and edit are menus.
newFile, openFile, saveFile, cut, copy, paste, selectAll, and close are menu items.
textArea is the area where text can be entered and displayed.

java
Copy code
TextEditor(){
//frame for the entire text editor
frame = new JFrame();

//initialize menubar
menuBar = new JMenuBar();

//text area for the project


textArea=new JTextArea();

//initialize the menus file and edit

file =new JMenu("File");


edit =new JMenu("Edit");
This is the constructor of the TextEditor class. It initializes the frame, menu
bar, and text area. It also initializes the file and edit menus.

java
Copy code
//initialize the menu items for the file
newFile = new JMenuItem("New Window");
openFile=new JMenuItem("Open File");
saveFile=new JMenuItem("Save File");

//add ActionListener to all these items


//so when clicked on this an actionEvent occurs and some action can be
preformed
newFile.addActionListener(this);
openFile.addActionListener(this);
saveFile.addActionListener(this);
//add the items to the menu file
file.add(newFile);
file.add(openFile);
file.add(saveFile);
Here, menu items for the file menu are initialized and added to the file menu.
Action listeners are added to each menu item so that they can respond to user
actions.

java
Copy code
//similarly for edit menu items
This part is similar to the previous one, but it initializes and adds menu items
for the edit menu.

java
Copy code
//set menu to menubar
menuBar.add(file);
menuBar.add(edit);

//set menubar to frame


frame.setJMenuBar(menuBar);
Here, the menus are added to the menu bar, and the menu bar is set to the frame.

java
Copy code
//create the panel and set its border and layout
JPanel panel=new JPanel();
panel.setBorder(new EmptyBorder(5,5,5,5));
panel.setLayout(new BorderLayout(0,0));

//add text area to panel


panel.add(textArea,BorderLayout.CENTER);
A panel is created to hold the text area. Its layout is set, and the text area is
added to it.

java
Copy code
//create a scroll pane with vertical and horizontal scrollbar
JScrollPane scrollPane=new
JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTA
L_SCROLLBAR_AS_NEEDED);
//add scroll pane to panel
panel.add(scrollPane);
A scroll pane is created to hold the text area, making it scrollable. The scroll
pane is then added to the panel.

java
Copy code
//add panel to frame
frame.add(panel);
The panel containing the text area and scroll pane is added to the frame.

java
Copy code
//set dimensions and layout of the frame
frame.setBounds(250,350,400,400);
frame.setTitle("TEXT EDITOR");
frame.setVisible(true);
frame.setLayout(null);
Finally, the dimensions, title, visibility, and layout of the frame are set.

The actionPerformed method and the main method handle actions and initialize the
text editor, respectively. Let me know if you need further explanation on any
specific part!

You might also like