Advertisement
andruhovski

TextEditor

Apr 9th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Container;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFileChooser;
  15. import javax.swing.JFrame;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextArea;
  20.  
  21. /*
  22.  * A simple Text Editor.  This demonstrates the use of a
  23.  * JFileChooser for the user to select a file to read from or write to.
  24.  * It also demonstrates reading and writing text files.
  25.  */
  26. public class TextEditor implements ActionListener {
  27.     // Size of editing text area.
  28.     private static final int NUM_ROWS = 25;
  29.     private static final int NUM_COLS = 50;
  30.  
  31.     // Buttons to save and load files.
  32.     private JButton saveButton, loadButton;
  33.  
  34.     // Area where the user does the editing
  35.     private JTextArea text;
  36.  
  37.     // Creates the GUI
  38.     public TextEditor() {
  39.         JFrame frame = new JFrame();
  40.         JPanel buttonPanel = new JPanel();
  41.         saveButton = new JButton("Save File");
  42.         loadButton = new JButton("Load File");
  43.         buttonPanel.add(saveButton);
  44.         buttonPanel.add(loadButton);
  45.  
  46.         text = new JTextArea(NUM_ROWS, NUM_COLS);
  47.         text.setFont(new Font("System", Font.PLAIN, 24));
  48.         JScrollPane textScroller = new JScrollPane(text);
  49.         Container contentPane = frame.getContentPane();
  50.         contentPane.add(textScroller, BorderLayout.CENTER);
  51.         contentPane.add(buttonPanel, BorderLayout.NORTH);
  52.  
  53.         saveButton.addActionListener(this);
  54.         loadButton.addActionListener(this);
  55.  
  56.         frame.pack();
  57.         frame.setVisible(true);
  58.     }
  59.  
  60.     // Listener for button clicks that loads the
  61.     // specified files and puts it in the
  62.     // editor.
  63.     public void actionPerformed(ActionEvent event) {
  64.         if (event.getSource() == saveButton) {
  65.             saveFile();
  66.         } else {
  67.             loadFile();
  68.         }
  69.     }
  70.  
  71.     // Display a file chooser so the user can select a file
  72.     // to save to. Then write the contents of the text area
  73.     // to that file. Does nothing if the user cancels out
  74.     // of the file chooser.
  75.     private void saveFile() {
  76.         File file;
  77.  
  78.         // create and display dialog box to get file name
  79.         JFileChooser dialog = new JFileChooser();
  80.  
  81.         // Make sure the user didn't cancel the file chooser
  82.         if (dialog.showSaveDialog(text) == JFileChooser.APPROVE_OPTION) {
  83.  
  84.             // Get the file the user selected
  85.             file = dialog.getSelectedFile();
  86.  
  87.             try {
  88.                 // Now write to the file
  89.                 PrintWriter output = new PrintWriter(new FileWriter(file));
  90.                 output.println(text.getText());
  91.                 output.close();
  92.             } catch (IOException e) {
  93.                 JOptionPane.showMessageDialog(text, "Can't save file "
  94.                         + e.getMessage());
  95.             }
  96.         }
  97.     }
  98.  
  99.     // Display a file chooser so the user can select a file to load.
  100.     // Then load the file into the editing area. Does nothing if
  101.     // the user cancels the file chooser.
  102.     private void loadFile() {
  103.         String line;
  104.         File file;
  105.  
  106.         // create and display dialog box to get file name
  107.         JFileChooser dialog = new JFileChooser();
  108.  
  109.         // Make sure the user did not cancel.
  110.         if (dialog.showOpenDialog(text) == JFileChooser.APPROVE_OPTION) {
  111.             // Find out which file the user selected.
  112.             file = dialog.getSelectedFile();
  113.  
  114.             try {
  115.                 // Open the file.
  116.                 BufferedReader input = new BufferedReader(new FileReader(file));
  117.  
  118.                 // Clear the editing area
  119.                 text.setText("");
  120.  
  121.                 // Fill up the ediitng area with the contents of the file being
  122.                 // read.
  123.                 line = input.readLine();
  124.                 while (line != null) {
  125.                     text.append(line + "\n");
  126.                     line = input.readLine();
  127.                 }
  128.  
  129.                 // Close the file
  130.                 input.close();
  131.             } catch (IOException e) {
  132.                 JOptionPane.showMessageDialog(text, "Can't load file "
  133.                         + e.getMessage());
  134.             }
  135.         }
  136.     }
  137.  
  138.     // Main program for the application
  139.     public static void main(String[] args) {
  140.         new TextEditor();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement