Texted It
Texted It
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
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();
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="";
}
}
}
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
}
}
}
if(actionEvent.getSource()==newFile){
TextEditor newTextEditor=new TextEditor();
}
}
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();
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");
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);
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));
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!