6. 6
// If a button ispressed
publicvoidactionPerformed(ActionEvente)
{
Strings = e.getActionCommand();
if (s.equals("cut")) {
t.cut();
}
else if (s.equals("copy")) {
t.copy();
}
else if (s.equals("paste")) {
t.paste();
}
else if (s.equals("Save")) {
// Create an objectof JFileChooserclass
JFileChooserj =new JFileChooser("f:");
// Invoke the showsSaveDialogfunctiontoshow the save dialog
intr = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
// Setthe label tothe path of the selecteddirectory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// Create a file writer
FileWriterwr= new FileWriter(fi,false);
7. 7
// Create bufferedwritertowrite
BufferedWriterw = new BufferedWriter(wr);
// Write
w.write(t.getText());
w.flush();
w.close();
}
catch (Exceptionevt) {
JOptionPane.showMessageDialog(f,evt.getMessage());
}
}
// If the user cancelledthe operation
else
JOptionPane.showMessageDialog(f,"the usercancelledthe operation");
}
else if (s.equals("Print")) {
try {
// printthe file
t.print();
}
catch (Exceptionevt) {
JOptionPane.showMessageDialog(f,evt.getMessage());
}
}
else if (s.equals("Open")) {
// Create an objectof JFileChooserclass
8. 8
JFileChooserj =new JFileChooser("f:");
// Invoke the showsOpenDialogfunctiontoshow the save dialog
intr = j.showOpenDialog(null);
// If the user selectsafile
if (r == JFileChooser.APPROVE_OPTION) {
// Setthe label tothe path of the selecteddirectory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// String
Strings1 = "", sl = "";
// File reader
FileReaderfr= new FileReader(fi);
// Bufferedreader
BufferedReaderbr= new BufferedReader(fr);
// Initilize sl
sl = br.readLine();
// Take the inputfrom the file
while ((s1= br.readLine())!=null) {
sl = sl + "n" + s1;
}
// Setthe text
9. 9
t.setText(sl);
}
catch (Exceptionevt) {
JOptionPane.showMessageDialog(f,evt.getMessage());
}
}
// If the user cancelledthe operation
else
JOptionPane.showMessageDialog(f,"the usercancelledthe operation");
}
else if (s.equals("New")) {
t.setText("");
}
else if (s.equals("close")) {
f.setVisible(false);
}
}
// Main class
publicstaticvoidmain(Stringargs[])
{
editore = neweditor();
}
}
https://www.geeksforgeeks.org/file-handling-java-using-filewriter-filereader/
https://www.geeksforgeeks.org/different-ways-reading-text-file-java/
https://www.geeksforgeeks.org/java-swing-create-a-simple-text-editor/
https://sites.google.com/site/tyroprogramming/java/text-editor