Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.io.*;
- import static javax.swing.JOptionPane.showInputDialog;
- public class Main {
- public static JFrame frame = new JFrame("MoPad"); // Set Window title
- public static String fileDir = "";
- public static void main(String[] args) {
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close when the 'X' is clicked
- frame.setSize(410,400); // Set window size
- JTextArea text = new JTextArea(); // Create Text Area
- JScrollPane scrollBar = new JScrollPane(text); // Add a scrollBar to the textArea
- text.setLineWrap(false); // Disable Line-Wrap
- text.setBorder(BorderFactory.createEmptyBorder(10, 7,10,10)); // Create empty space between the text box and window
- Font textFont = new Font("Comic Sans MS", Font.BOLD, 15); // Create the font object
- text.setFont(textFont); // Set Text Font
- JMenuBar menuBar = addMenus(text);
- frame.setJMenuBar(menuBar);
- frame.getContentPane().add(scrollBar); // Adds both the textArea, and the scroll bar
- frame.setVisible(true);
- }
- public static String getFileDir() {
- JFileChooser openDialog = new JFileChooser();
- openDialog.setCurrentDirectory(new File(System.getProperty("user.home")));
- int result = openDialog.showOpenDialog(null);
- if (result == JFileChooser.APPROVE_OPTION) {
- File selectedFile = openDialog.getSelectedFile();
- return selectedFile.getAbsolutePath();
- }
- else {
- return "error";
- }
- }
- public static String readFile(String dir, JTextArea text) throws IOException {
- FileInputStream file = null;
- String fileContents = "";
- try {
- file = new FileInputStream(dir);
- int c;
- while ((c = file.read()) != -1) {
- fileContents += (char) c;
- }
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null,
- "The system could not find the file specified",
- "Error",
- JOptionPane.ERROR_MESSAGE);
- e.printStackTrace();
- } finally {
- file.close();
- }
- System.out.println(fileContents + "\n\n");
- text.setText(fileContents);
- return fileContents;
- }
- public static void saveFile(String text, String dir) {
- FileOutputStream file = null;
- try {
- file = new FileOutputStream(dir);
- file.write(text.getBytes());
- } catch(FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static String getNewFileDir() {
- return JOptionPane.showInputDialog("New File Directory");
- }
- public static void createFile(String newFileDir) {
- File newFile = new File(newFileDir);
- newFile.getParentFile().mkdirs();
- try {
- newFile.createNewFile();
- fileDir = newFileDir;
- } catch (IOException e) {
- JOptionPane.showMessageDialog(null,
- "Invalid File Directory, try again",
- "Invalid Directory",
- JOptionPane.ERROR_MESSAGE);
- e.printStackTrace();
- }
- }
- public static JMenuBar addMenus(JTextArea text) {
- JMenuBar menuBar = new JMenuBar();
- JMenu fileOptions = new JMenu("File");
- JMenuItem newFile = new JMenuItem("New");
- newFile.addActionListener(e -> {
- String newFileDir = getNewFileDir();
- createFile(newFileDir);
- frame.setTitle("MoPad - " + fileDir);
- });
- JMenuItem open = new JMenuItem("Open");
- open.addActionListener(e -> {
- fileDir = getFileDir();
- if(fileDir == "error") {
- fileDir = "";
- }
- try {
- readFile(fileDir, text);
- } catch (IOException err) {
- err.printStackTrace();
- }
- frame.setTitle("MoPad - " + fileDir);
- });
- JMenuItem save = new JMenuItem("Save");
- save.addActionListener(e -> {
- if(fileDir == "") {
- JOptionPane.showMessageDialog(null,
- "No file has been opened",
- "No file",
- JOptionPane.WARNING_MESSAGE);
- } else {
- String strText = text.getText();
- saveFile(strText, fileDir);
- JOptionPane.showMessageDialog(null,
- "The file (" + fileDir + ") has been saved",
- "File Saved",
- JOptionPane.INFORMATION_MESSAGE);
- }
- });
- JMenu helpOptions = new JMenu("Help");
- JMenuItem about = new JMenuItem("About");
- about.addActionListener(e -> {
- JOptionPane.showMessageDialog(null,
- "Simple Text Editor written in Java using the Swing GUI Toolkit",
- "About",
- JOptionPane.INFORMATION_MESSAGE);
- });
- fileOptions.add(newFile);
- fileOptions.add(open);
- fileOptions.add(save);
- helpOptions.add(about);
- menuBar.add(fileOptions);
- menuBar.add(helpOptions);
- return menuBar;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement