Advertisement
Guest User

Text Editor

a guest
Apr 6th, 2020
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.65 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.*;
  4.  
  5. import static javax.swing.JOptionPane.showInputDialog;
  6.  
  7. public class Main {
  8.  
  9.     public static JFrame frame = new JFrame("MoPad"); // Set Window title
  10.     public static String fileDir = "";
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close when the 'X' is clicked
  15.         frame.setSize(410,400); // Set window size
  16.  
  17.  
  18.         JTextArea text = new JTextArea(); // Create Text Area
  19.         JScrollPane scrollBar = new JScrollPane(text); // Add a scrollBar to the textArea
  20.  
  21.         text.setLineWrap(false); // Disable Line-Wrap
  22.         text.setBorder(BorderFactory.createEmptyBorder(10, 7,10,10)); // Create empty space between the text box and window
  23.  
  24.         Font textFont = new Font("Comic Sans MS", Font.BOLD, 15); // Create the font object
  25.         text.setFont(textFont); // Set Text Font
  26.  
  27.         JMenuBar menuBar = addMenus(text);
  28.  
  29.         frame.setJMenuBar(menuBar);
  30.         frame.getContentPane().add(scrollBar); // Adds both the textArea, and the scroll bar
  31.         frame.setVisible(true);
  32.  
  33.     }
  34.  
  35.     public static String getFileDir() {
  36.         JFileChooser openDialog = new JFileChooser();
  37.         openDialog.setCurrentDirectory(new File(System.getProperty("user.home")));
  38.  
  39.         int result = openDialog.showOpenDialog(null);
  40.  
  41.         if (result == JFileChooser.APPROVE_OPTION) {
  42.             File selectedFile = openDialog.getSelectedFile();
  43.             return selectedFile.getAbsolutePath();
  44.         }
  45.         else {
  46.             return "error";
  47.         }
  48.     }
  49.  
  50.     public static String readFile(String dir, JTextArea text) throws IOException {
  51.         FileInputStream file = null;
  52.         String fileContents = "";
  53.  
  54.  
  55.         try {
  56.             file = new FileInputStream(dir);
  57.  
  58.             int c;
  59.             while ((c = file.read()) != -1) {
  60.                 fileContents += (char) c;
  61.             }
  62.         } catch (FileNotFoundException e) {
  63.             JOptionPane.showMessageDialog(null,
  64.                     "The system could not find the file specified",
  65.                     "Error",
  66.                     JOptionPane.ERROR_MESSAGE);
  67.             e.printStackTrace();
  68.         } finally {
  69.             file.close();
  70.         }
  71.  
  72.         System.out.println(fileContents + "\n\n");
  73.  
  74.         text.setText(fileContents);
  75.         return fileContents;
  76.     }
  77.  
  78.     public static void saveFile(String text, String dir) {
  79.         FileOutputStream file = null;
  80.  
  81.         try {
  82.             file = new FileOutputStream(dir);
  83.             file.write(text.getBytes());
  84.         } catch(FileNotFoundException e) {
  85.             e.printStackTrace();
  86.         } catch (IOException e) {
  87.             e.printStackTrace();
  88.         } finally {
  89.             try {
  90.                 file.close();
  91.             } catch (IOException e) {
  92.                 e.printStackTrace();
  93.             }
  94.         }
  95.     }
  96.  
  97.     public static String getNewFileDir() {
  98.          return JOptionPane.showInputDialog("New File Directory");
  99.     }
  100.  
  101.     public static void createFile(String newFileDir) {
  102.         File newFile = new File(newFileDir);
  103.  
  104.  
  105.         newFile.getParentFile().mkdirs();
  106.  
  107.         try {
  108.             newFile.createNewFile();
  109.             fileDir = newFileDir;
  110.         } catch (IOException e) {
  111.             JOptionPane.showMessageDialog(null,
  112.                     "Invalid File Directory, try again",
  113.                     "Invalid Directory",
  114.                     JOptionPane.ERROR_MESSAGE);
  115.             e.printStackTrace();
  116.         }
  117.     }
  118.  
  119.     public static JMenuBar addMenus(JTextArea text) {
  120.         JMenuBar menuBar = new JMenuBar();
  121.  
  122.         JMenu fileOptions = new JMenu("File");
  123.  
  124.         JMenuItem newFile = new JMenuItem("New");
  125.         newFile.addActionListener(e -> {
  126.             String newFileDir = getNewFileDir();
  127.             createFile(newFileDir);
  128.  
  129.             frame.setTitle("MoPad - " + fileDir);
  130.         });
  131.  
  132.         JMenuItem open = new JMenuItem("Open");
  133.         open.addActionListener(e -> {
  134.             fileDir = getFileDir();
  135.             if(fileDir == "error") {
  136.                 fileDir = "";
  137.             }
  138.             try {
  139.                 readFile(fileDir, text);
  140.             } catch (IOException err) {
  141.                 err.printStackTrace();
  142.             }
  143.             frame.setTitle("MoPad - " + fileDir);
  144.         });
  145.  
  146.         JMenuItem save = new JMenuItem("Save");
  147.         save.addActionListener(e -> {
  148.             if(fileDir == "") {
  149.                 JOptionPane.showMessageDialog(null,
  150.                         "No file has been opened",
  151.                         "No file",
  152.                         JOptionPane.WARNING_MESSAGE);
  153.             } else {
  154.                 String strText = text.getText();
  155.                 saveFile(strText, fileDir);
  156.                 JOptionPane.showMessageDialog(null,
  157.                         "The file (" + fileDir + ") has been saved",
  158.                         "File Saved",
  159.                         JOptionPane.INFORMATION_MESSAGE);
  160.             }
  161.         });
  162.  
  163.         JMenu helpOptions = new JMenu("Help");
  164.  
  165.         JMenuItem about = new JMenuItem("About");
  166.         about.addActionListener(e -> {
  167.             JOptionPane.showMessageDialog(null,
  168.                     "Simple Text Editor written in Java using the Swing GUI Toolkit",
  169.                     "About",
  170.                     JOptionPane.INFORMATION_MESSAGE);
  171.         });
  172.  
  173.         fileOptions.add(newFile);
  174.         fileOptions.add(open);
  175.         fileOptions.add(save);
  176.  
  177.         helpOptions.add(about);
  178.  
  179.         menuBar.add(fileOptions);
  180.         menuBar.add(helpOptions);
  181.  
  182.         return menuBar;
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement