Advertisement
kuchuz

PBO-C 10 : TextEditor

Jan 11th, 2021
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.io.*;
  4. import java.awt.event.*;
  5. import javax.swing.plaf.metal.*;
  6. import javax.swing.text.*;
  7.  
  8. public class TextEditor extends JFrame implements ActionListener
  9. {
  10.     // Text component
  11.    
  12.     JTextArea t;
  13.    
  14.     // Frame
  15.    
  16.     JFrame f;
  17.    
  18.     // Constructor
  19.    
  20.     TextEditor()
  21.     {
  22.         // Create a frame
  23.        
  24.         f = new JFrame("Text Editor");
  25.        
  26.         try
  27.         {
  28.             // Set metal look and feel
  29.            
  30.             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  31.            
  32.             // Set theme to ocean
  33.            
  34.             MetalLookAndFeel.setCurrentTheme(new OceanTheme());
  35.         } catch (Exception e) {
  36.            
  37.         }
  38.        
  39.         // Text component
  40.        
  41.         t = new JTextArea();
  42.        
  43.         // Create a menubar
  44.        
  45.         JMenuBar mb = new JMenuBar();
  46.        
  47.         // Create a menu for menu
  48.        
  49.         JMenu m1 = new JMenu("File");
  50.        
  51.         // Create menu items
  52.        
  53.         JMenuItem mi1 = new JMenuItem("New");
  54.         JMenuItem mi2 = new JMenuItem("Open");
  55.         JMenuItem mi3 = new JMenuItem("Save");
  56.         JMenuItem mi9 = new JMenuItem("Print");
  57.        
  58.         // Add action listener
  59.        
  60.         mi1.addActionListener(this);
  61.         mi2.addActionListener(this);
  62.         mi3.addActionListener(this);
  63.         mi9.addActionListener(this);
  64.        
  65.         m1.add(mi1);
  66.         m1.add(mi2);
  67.         m1.add(mi3);
  68.         m1.add(mi9);
  69.        
  70.         // Create a menu for menu
  71.        
  72.         JMenu m2 = new JMenu("Edit");
  73.        
  74.         // Create menu items
  75.        
  76.         JMenuItem mi4 = new JMenuItem("Cut");
  77.         JMenuItem mi5 = new JMenuItem("Copy");
  78.         JMenuItem mi6 = new JMenuItem("Paste");
  79.        
  80.         // Add action listener
  81.        
  82.         mi4.addActionListener(this);
  83.         mi5.addActionListener(this);
  84.         mi6.addActionListener(this);
  85.        
  86.         m2.add(mi4);
  87.         m2.add(mi5);
  88.         m2.add(mi6);
  89.        
  90.         JMenuItem mc = new JMenuItem("Close");
  91.        
  92.         mc.addActionListener(this);
  93.        
  94.         mb.add(m1);
  95.         mb.add(m2);
  96.         mb.add(mc);
  97.        
  98.         f.setJMenuBar(mb);
  99.         f.add(t);
  100.         f.setSize(500, 500);
  101.         f.show();
  102.     }
  103.    
  104.     public void actionPerformed(ActionEvent e)
  105.     {
  106.         String s = e.getActionCommand().toLowerCase();
  107.        
  108.         if (s.equals("cut"))
  109.         {
  110.             t.cut();
  111.         } else if (s.equals("copy")) {
  112.             t.copy();
  113.         } else if (s.equals("paste")) {
  114.             t.paste();
  115.         } else if (s.equals("save")) {
  116.             // Create an object of JFileChooser class
  117.            
  118.             JFileChooser j = new JFileChooser("c:");
  119.            
  120.             // Invoke the showSaveDialog function to show the save dialog
  121.            
  122.             int r = j.showSaveDialog(null);
  123.            
  124.             if (r == JFileChooser.APPROVE_OPTION) {
  125.                 // Set the label to the path of the selected directory
  126.                
  127.                 File fi = new File(j.getSelectedFile().getAbsolutePath());
  128.                
  129.                 try
  130.                 {
  131.                     // Create a file writer
  132.                    
  133.                     FileWriter wr = new FileWriter(fi, false);
  134.                    
  135.                     // Create buffered writer to write
  136.                    
  137.                     BufferedWriter w = new BufferedWriter(wr);
  138.                    
  139.                     // Write
  140.                    
  141.                     w.write(t.getText());
  142.                    
  143.                     w.flush();
  144.                     w.close();
  145.                 } catch (Exception evt) {
  146.                     JOptionPane.showMessageDialog(f, evt.getMessage());
  147.                 }
  148.             } else JOptionPane.showMessageDialog(f, "The user cancelled the operation"); // If the user cancelled the operation
  149.         } else if (s.equals("print")) {
  150.             try {
  151.                 // Print the file
  152.                
  153.                 t.print();
  154.             } catch (Exception evt) {
  155.                 JOptionPane.showMessageDialog(f, evt.getMessage());
  156.             }
  157.         } else if (s.equals("open")) {
  158.             // Create an object of JFileChooser class
  159.            
  160.             JFileChooser j = new JFileChooser("c:");
  161.            
  162.             // Invoke the showOpenDialog function to show the open dialog
  163.            
  164.             int r = j.showOpenDialog(null);
  165.            
  166.             // If the user selects a file
  167.            
  168.             if (r == JFileChooser.APPROVE_OPTION)
  169.             {
  170.                 // Set the label to the path of the selected directory
  171.                
  172.                 File fi = new File(j.getSelectedFile().getAbsolutePath());
  173.                
  174.                 try
  175.                 {
  176.                     // String
  177.                    
  178.                     String s1 = "", sl = "";
  179.                    
  180.                     // File reader
  181.                    
  182.                     FileReader fr = new FileReader(fi);
  183.                    
  184.                     // Buffered reader
  185.                    
  186.                     BufferedReader br = new BufferedReader(fr);
  187.                    
  188.                     // Initialize sl
  189.                    
  190.                     sl = br.readLine();
  191.                    
  192.                     // Take the input from the file
  193.                    
  194.                     while ((s1 = br.readLine()) != null)
  195.                     {
  196.                         sl = sl + "\n" + s1;
  197.                     }
  198.                    
  199.                     // Set the text
  200.                    
  201.                     t.setText(sl);
  202.                 } catch (Exception evt) {
  203.                     JOptionPane.showMessageDialog(f, evt.getMessage());
  204.                 }
  205.             } else JOptionPane.showMessageDialog(f, "The user cancelled the operation"); // If the user cancelled the operation
  206.         } else if (s.equals("new")) {
  207.             t.setText("");
  208.         } else if (s.equals("close")) {
  209.             f.setVisible(false);
  210.         }
  211.     }
  212.    
  213.     public static void main(String[] args)
  214.     {
  215.         TextEditor te = new TextEditor();
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement