Text Editor

    Pada projek kali ini saya membuat sebuah text editor yang dimana cara kerja text editor ini tidak jauh dari aplikasi notepad yang dapat mengedit sebuah file dalam bentuk text. Di dalam program bisa membuka file yang sudah dibuat sebelumnya, membuat file yang baru, dan menyimpan file yang telah di edit atau baru saja dibuat. Untuk membuat program tersebut diagram classnya :


Source Code :


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class Notepad extends JFrame implements ActionListener {
private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar(); // menu bar
private Menu file = new Menu(); // menu file
private MenuItem openFile = new MenuItem(); // open option
private MenuItem saveFile = new MenuItem(); // save option
private MenuItem close = new MenuItem(); // close option
private MenuItem newFile = new MenuItem(); // new option
public Notepad() {
this.setSize(500, 300); // window's size
this.setTitle("Java Text Editor"); // window's title
setDefaultCloseOperation(EXIT_ON_CLOSE); // close button
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // font in TextArea
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
// adds menu bar in gui
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);
// adds file menu
this.file.setLabel("File");
// open option
this.openFile.setLabel("Open"); // label
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // shortcut ctrl + o
this.file.add(this.openFile); // putting open option into file menu
// save option
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false)); // shortcut ctrl + s
this.file.add(this.saveFile);
// close option
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
// new option
this.newFile.setLabel("New");
this.newFile.addActionListener(this);
this.newFile.setShortcut(new MenuShortcut(KeyEvent.VK_N, false)); // shortcut ctrl + n
this.file.add(this.newFile);
}
public void actionPerformed (ActionEvent e) {
// if we pick close option
if (e.getSource() == this.close)
this.dispose(); //the data inside the session will be erased and the application will stop working
// if we pick open option
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // opening file chooser (a dialog will be shown to choose a file)
int option = open.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
this.textArea.setText(""); // cleans the textarea before opening a new file
try {
// making a scanner that will read the file (getSelectedFile().getPath() will get the path to the file)
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) //
this.textArea.append(scan.nextLine() + "\n");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
// if we pick save option
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser(); // opening file chooser
int option = save.showSaveDialog(this); // same as open option but we call showSaveDialog
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // writing a content inside TextArea to file
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
// if we pick new option
else if (e.getSource() == this.newFile) {
textArea.setText("");
}
}
// main method
public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


Output :
Tampilan awal



Menu yang di sediakan




Tampilan jika ingin membuka sebuah file




File yang telah dibuka


Jika membuat sebuah file baru dan menyimpan file yang telah dibuat





File yang telah tersimpan akan masuk ke folder yang dituju oleh pengguna 




Comments

Popular Posts