DarthVictor

JavaNotePad

Apr 12th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import javax.swing.JFileChooser;
  8. import javax.swing.JOptionPane;
  9.  
  10. /*
  11.  * Editor.java
  12.  *
  13.  * Created on 09/10/2010, 16:36:20
  14.  */
  15.  
  16. /**
  17.  *
  18.  * @author Victor Mello Floriano
  19.  */
  20. public class EditorDeTexto extends javax.swing.JFrame {
  21.  
  22.     /** Creates new form Editor */
  23.     public EditorDeTexto() {
  24.         initComponents();
  25.     }
  26.  
  27.     /** This method is called from within the constructor to
  28.      * initialize the form.
  29.      * WARNING: Do NOT modify this code. The content of this method is
  30.      * always regenerated by the Form Editor.
  31.      */
  32.     @SuppressWarnings("unchecked")
  33.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  34.     private void initComponents() {
  35.  
  36.         jPanel1 = new javax.swing.JPanel();
  37.         jScrollPane1 = new javax.swing.JScrollPane();
  38.         jTextArea1 = new javax.swing.JTextArea();
  39.         jMenuBar1 = new javax.swing.JMenuBar();
  40.         jMenu1 = new javax.swing.JMenu();
  41.         jMenuItem1 = new javax.swing.JMenuItem();
  42.         jMenuItem2 = new javax.swing.JMenuItem();
  43.         jMenu2 = new javax.swing.JMenu();
  44.         jMenuItem3 = new javax.swing.JMenuItem();
  45.  
  46.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  47.  
  48.         jTextArea1.setColumns(20);
  49.         jTextArea1.setRows(5);
  50.         jScrollPane1.setViewportView(jTextArea1);
  51.  
  52.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  53.         jPanel1.setLayout(jPanel1Layout);
  54.         jPanel1Layout.setHorizontalGroup(
  55.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  56.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
  57.         );
  58.         jPanel1Layout.setVerticalGroup(
  59.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  60.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
  61.         );
  62.  
  63.         jMenu1.setText("Options");
  64.  
  65.         jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
  66.         jMenuItem1.setText("Save");
  67.         jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 jMenuItem1ActionPerformed(evt);
  70.             }
  71.         });
  72.         jMenu1.add(jMenuItem1);
  73.  
  74.         jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
  75.         jMenuItem2.setText("Open");
  76.         jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
  77.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  78.                 jMenuItem2ActionPerformed(evt);
  79.             }
  80.         });
  81.         jMenu1.add(jMenuItem2);
  82.  
  83.         jMenuBar1.add(jMenu1);
  84.  
  85.         jMenu2.setText("Help");
  86.  
  87.         jMenuItem3.setText("Report a bug");
  88.         jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
  89.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  90.                 jMenuItem3ActionPerformed(evt);
  91.             }
  92.         });
  93.         jMenu2.add(jMenuItem3);
  94.  
  95.         jMenuBar1.add(jMenu2);
  96.  
  97.         setJMenuBar(jMenuBar1);
  98.  
  99.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  100.         getContentPane().setLayout(layout);
  101.         layout.setHorizontalGroup(
  102.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  104.         );
  105.         layout.setVerticalGroup(
  106.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  107.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  108.         );
  109.  
  110.         pack();
  111.     }// </editor-fold>//GEN-END:initComponents
  112.  
  113.     private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
  114.         try {
  115.             Save();
  116.         } catch (FileNotFoundException ex) {
  117.              JOptionPane.showMessageDialog(null, "FileNotFoundException");
  118.         } catch (IOException ex) {
  119.           JOptionPane.showMessageDialog(null, "IOException"); }
  120.     }//GEN-LAST:event_jMenuItem1ActionPerformed
  121.  
  122.     private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
  123.         try {
  124.             Open();
  125.         } catch (FileNotFoundException ex) {
  126.            JOptionPane.showMessageDialog(null, "FileNotFoundException");
  127.         } catch (IOException ex) {
  128.           JOptionPane.showMessageDialog(null, "IOException");
  129.         }
  130.     }//GEN-LAST:event_jMenuItem2ActionPerformed
  131.  
  132.     private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
  133. JOptionPane.showMessageDialog(null, "contact [email protected] \n to report a bug");
  134.     }//GEN-LAST:event_jMenuItem3ActionPerformed
  135.  
  136.     /**
  137.     * @param args the command line arguments
  138.     */
  139.     public static void main(String args[]) {
  140.         java.awt.EventQueue.invokeLater(new Runnable() {
  141.             public void run() {
  142.                 new EditorDeTexto().setVisible(true);
  143.             }
  144.         });
  145.     }
  146.  
  147.     // Variables declaration - do not modify//GEN-BEGIN:variables
  148.     private javax.swing.JMenu jMenu1;
  149.     private javax.swing.JMenu jMenu2;
  150.     private javax.swing.JMenuBar jMenuBar1;
  151.     private javax.swing.JMenuItem jMenuItem1;
  152.     private javax.swing.JMenuItem jMenuItem2;
  153.     private javax.swing.JMenuItem jMenuItem3;
  154.     private javax.swing.JPanel jPanel1;
  155.     private javax.swing.JScrollPane jScrollPane1;
  156.     static javax.swing.JTextArea jTextArea1;
  157.     // End of variables declaration//GEN-END:variables
  158.  
  159.  
  160.  
  161.     public  static  void Save () throws  FileNotFoundException, IOException {
  162.    JFileChooser chooser = new JFileChooser();
  163.  
  164.     //Abre o file chooser. Ele retorna JFileChooser.APPROVE_OPTION caso o usuário
  165.     //pressione ok.
  166.     if  (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
  167.         return;  //Se não pressionou ok, saímos do método.
  168.     }
  169.  
  170.     //Usamos o getSelectedFile() para pegar o nome do arquivo selecionado
  171.     PrintWriter pw = new PrintWriter (chooser.getSelectedFile());
  172.     pw.println (jTextArea1.getText());
  173.     pw.flush();
  174.     pw.close();
  175.  }
  176.  
  177.       public  static  void Open () throws  FileNotFoundException, IOException {
  178. JFileChooser loadEmp = new JFileChooser();//new dialog
  179.  
  180.     //Abre o file chooser. Ele retorna JFileChooser.APPROVE_OPTION caso o usuário
  181.     //pressione ok.
  182.     if  (loadEmp.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
  183.         return;  //Se não pressionou ok, saímos do método.
  184.     }
  185. FileReader fr = new FileReader(loadEmp.getSelectedFile());
  186. BufferedReader br = new BufferedReader(fr);
  187. String linha;
  188. StringBuilder sb = new StringBuilder();
  189. while((linha = br.readLine()) != null) {
  190. sb.append(linha).append("\n");
  191. }
  192. fr.close();
  193. jTextArea1.setText(sb.toString());
  194.  
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment