GuilhermeRamalho

Simple Text Editor Java

May 1st, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.67 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package texteditor;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.swing.JOptionPane;
  18.  
  19.  
  20. /**
  21.  *
  22.  * @author Guilherme
  23.  */
  24. public class mainScreen extends javax.swing.JFrame {
  25.  
  26.     private static void setExtendedState() {
  27.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  28.     }
  29.  
  30.     /**
  31.      * Creates new form mainScreen
  32.      */
  33.     public mainScreen() {
  34.         initComponents();
  35.         saveFile.setEnabled(false);
  36.         closeFile.setEnabled(false);
  37.         editorPane.setVisible(false);
  38.        
  39.     }
  40.  
  41.     /**
  42.      * This method is called from within the constructor to initialize the form.
  43.      * WARNING: Do NOT modify this code. The content of this method is always
  44.      * regenerated by the Form Editor.
  45.      */
  46.     @SuppressWarnings("unchecked")
  47.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  48.     private void initComponents() {
  49.  
  50.         panel = new javax.swing.JPanel();
  51.         newFile = new javax.swing.JButton();
  52.         openFile = new javax.swing.JButton();
  53.         saveFile = new javax.swing.JButton();
  54.         closeFile = new javax.swing.JButton();
  55.         exit = new javax.swing.JButton();
  56.         jScrollPane1 = new javax.swing.JScrollPane();
  57.         editorPane = new javax.swing.JEditorPane();
  58.         surpriseMotherfucker = new javax.swing.JButton();
  59.  
  60.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  61.         setLocation(new java.awt.Point(0, 0));
  62.  
  63.         newFile.setText("Novo");
  64.         newFile.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 newFileActionPerformed(evt);
  67.             }
  68.         });
  69.  
  70.         openFile.setText("Abrir");
  71.         openFile.addActionListener(new java.awt.event.ActionListener() {
  72.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  73.                 openFileActionPerformed(evt);
  74.             }
  75.         });
  76.  
  77.         saveFile.setText("Salvar");
  78.         saveFile.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 saveFileActionPerformed(evt);
  81.             }
  82.         });
  83.  
  84.         closeFile.setText("Fechar");
  85.         closeFile.addActionListener(new java.awt.event.ActionListener() {
  86.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  87.                 closeFileActionPerformed(evt);
  88.             }
  89.         });
  90.  
  91.         exit.setText("Finalizar");
  92.         exit.addActionListener(new java.awt.event.ActionListener() {
  93.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  94.                 exitActionPerformed(evt);
  95.             }
  96.         });
  97.  
  98.         editorPane.addKeyListener(new java.awt.event.KeyAdapter() {
  99.             public void keyTyped(java.awt.event.KeyEvent evt) {
  100.                 editorPaneKeyTyped(evt);
  101.             }
  102.         });
  103.         jScrollPane1.setViewportView(editorPane);
  104.  
  105.         surpriseMotherfucker.setText("jButton1");
  106.         surpriseMotherfucker.addActionListener(new java.awt.event.ActionListener() {
  107.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  108.                 surpriseMotherfuckerActionPerformed(evt);
  109.             }
  110.         });
  111.  
  112.         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
  113.         panel.setLayout(panelLayout);
  114.         panelLayout.setHorizontalGroup(
  115.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  116.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup()
  117.                 .addContainerGap()
  118.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  119.                     .addGroup(panelLayout.createSequentialGroup()
  120.                         .addGap(0, 0, Short.MAX_VALUE)
  121.                         .addComponent(surpriseMotherfucker, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
  122.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
  123.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelLayout.createSequentialGroup()
  124.                         .addComponent(newFile)
  125.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  126.                         .addComponent(openFile)
  127.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  128.                         .addComponent(saveFile)
  129.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  130.                         .addComponent(closeFile)
  131.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  132.                         .addComponent(exit)))
  133.                 .addGap(16, 16, 16))
  134.         );
  135.  
  136.         panelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {closeFile, exit, newFile, openFile, saveFile});
  137.  
  138.         panelLayout.setVerticalGroup(
  139.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  140.             .addGroup(panelLayout.createSequentialGroup()
  141.                 .addGap(11, 11, 11)
  142.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  143.                     .addComponent(newFile)
  144.                     .addComponent(openFile)
  145.                     .addComponent(saveFile)
  146.                     .addComponent(closeFile)
  147.                     .addComponent(exit))
  148.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  149.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
  150.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
  151.                 .addComponent(surpriseMotherfucker, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
  152.         );
  153.  
  154.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  155.         getContentPane().setLayout(layout);
  156.         layout.setHorizontalGroup(
  157.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  158.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  159.         );
  160.         layout.setVerticalGroup(
  161.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  162.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  163.         );
  164.  
  165.         pack();
  166.         setLocationRelativeTo(null);
  167.     }// </editor-fold>                        
  168.  
  169.    
  170.    
  171.     File file = new File("C:/texto.txt");
  172.    
  173.    
  174.     private void newFileActionPerformed(java.awt.event.ActionEvent evt) {                                        
  175.         // TODO add your handling code here:
  176.         //String path = JOptionPane.showInputDialog("Digite o caminho onde deseja salvar o arquivo: ");
  177.        
  178.         try {
  179.             file.createNewFile();
  180.             System.out.println("Arquivo 'texto' criado com sucesso.");
  181.            
  182.             saveFile.setEnabled(true);
  183.             System.out.println("Botão salvar ativado!");
  184.            
  185.             editorPane.setVisible(true);
  186.             System.out.println("Editor de texto liberado");
  187.            
  188.         } catch (IOException ex) {
  189.             Logger.getLogger(mainScreen.class.getName()).log(Level.SEVERE, null, ex);
  190.         }
  191.     }                                      
  192.  
  193.     private void openFileActionPerformed(java.awt.event.ActionEvent evt) {                                        
  194.         try {
  195.             // TODO add your handling code here:
  196.             FileReader fr = new FileReader(file);
  197.             BufferedReader br = new BufferedReader(fr);
  198.            
  199.             newFile.setEnabled(false);
  200.             System.out.println("Botão novo desativado!");
  201.            
  202.             saveFile.setEnabled(true);
  203.             System.out.println("Botão salvar ativado!");
  204.            
  205.             closeFile.setEnabled(true);
  206.             System.out.println("Botão fechar ativado!");
  207.            
  208.             editorPane.setVisible(true);
  209.             System.out.println("Editor de texto liberado!");
  210.            
  211.             while(br.ready())
  212.             {
  213.                 String line = br.readLine();
  214.                 editorPane.setText(line);
  215.             }
  216.             br.close();
  217.             fr.close();
  218.            
  219.         } catch (FileNotFoundException ex) {
  220.             Logger.getLogger(mainScreen.class.getName()).log(Level.SEVERE, null, ex);
  221.         } catch (IOException ex) {
  222.             Logger.getLogger(mainScreen.class.getName()).log(Level.SEVERE, null, ex);
  223.         }
  224.     }                                        
  225.  
  226.     private void saveFileActionPerformed(java.awt.event.ActionEvent evt) {                                        
  227.        
  228.         try {
  229.             FileWriter fw = new FileWriter(file);
  230.             BufferedWriter bw = new BufferedWriter(fw);
  231.            
  232.             bw.write(editorPane.getText());
  233.             bw.close();
  234.             System.out.println("Texto salvo com sucesso!(Teoricamente :D)");
  235.             JOptionPane.showMessageDialog(null, "Arquivo salvo com sucesso!");
  236.            
  237.             closeFile.setEnabled(true);
  238.             System.out.println("Botão fechar ativado!");
  239.            
  240.         } catch (IOException ex) {
  241.             Logger.getLogger(mainScreen.class.getName()).log(Level.SEVERE, null, ex);
  242.         }
  243.        
  244.        
  245.         closeFile.setEnabled(true);
  246.         System.out.println("Botão fechar ativado!");
  247.     }                                        
  248.  
  249.     private void closeFileActionPerformed(java.awt.event.ActionEvent evt) {                                          
  250.         // TODO add your handling code here:
  251.         try {
  252.             FileWriter fw = new FileWriter(file);
  253.             BufferedWriter bw = new BufferedWriter(fw);
  254.            
  255.             bw.write(editorPane.getText());
  256.             bw.close();
  257.             System.out.println("Texto salvo com sucesso!(Teoricamente :D)");
  258.             JOptionPane.showMessageDialog(null, "Arquivo salvo com sucesso!");
  259.            
  260.             saveFile.setEnabled(false);
  261.             System.out.println("Botão salvar desativado!");
  262.            
  263.             closeFile.setEnabled(false);
  264.             System.out.println("Botão fechar desativado!");
  265.            
  266.            
  267.         } catch (IOException ex) {
  268.             Logger.getLogger(mainScreen.class.getName()).log(Level.SEVERE, null, ex);
  269.         }
  270.     }                                        
  271.  
  272.     private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                    
  273.         // TODO add your handling code here:
  274.         dispose();
  275.     }                                    
  276.  
  277.     private void editorPaneKeyTyped(java.awt.event.KeyEvent evt) {                                    
  278.         // TODO add your handling code here:
  279.         String text = editorPane.getText();
  280.         System.out.println("Testo sendo digitado: "+text);
  281.     }                                  
  282.  
  283.     private void surpriseMotherfuckerActionPerformed(java.awt.event.ActionEvent evt) {                                                    
  284.         // TODO add your handling code here:
  285.         JOptionPane.showMessageDialog(null, "Esse botão aqui foi feito pra você curioso que "
  286.                 + "não sabe o que o botão faz e já vai apertando.\n"
  287.                 + " Se não fosse por minha boa natureza agora "
  288.                 + "você teria cagado todo software.\n\n"
  289.                 + "Guilherme Ramalho");
  290.     }                                                    
  291.  
  292.     /**
  293.      * @param args the command line arguments
  294.      */
  295.     public static void main(String args[]) {
  296.         /* Set the Nimbus look and feel */
  297.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  298.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  299.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  300.          */
  301.         try {
  302.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  303.                 if ("Metal".equals(info.getName())) {
  304.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  305.                     break;
  306.                 }
  307.             }
  308.         } catch (ClassNotFoundException ex) {
  309.             java.util.logging.Logger.getLogger(mainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  310.         } catch (InstantiationException ex) {
  311.             java.util.logging.Logger.getLogger(mainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  312.         } catch (IllegalAccessException ex) {
  313.             java.util.logging.Logger.getLogger(mainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  314.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  315.             java.util.logging.Logger.getLogger(mainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  316.         }
  317.         //</editor-fold>
  318.  
  319.         /* Create and display the form */
  320.         java.awt.EventQueue.invokeLater(new Runnable() {
  321.             public void run() {
  322.                 new mainScreen().setVisible(true);
  323.             }
  324.         });
  325.     }
  326.  
  327.     // Variables declaration - do not modify                    
  328.     private javax.swing.JButton closeFile;
  329.     private javax.swing.JEditorPane editorPane;
  330.     private javax.swing.JButton exit;
  331.     private javax.swing.JScrollPane jScrollPane1;
  332.     private javax.swing.JButton newFile;
  333.     private javax.swing.JButton openFile;
  334.     private javax.swing.JPanel panel;
  335.     private javax.swing.JButton saveFile;
  336.     private javax.swing.JButton surpriseMotherfucker;
  337.     // End of variables declaration                  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment