Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- /*
- * Editor.java
- *
- * Created on 09/10/2010, 16:36:20
- */
- /**
- *
- * @author Victor Mello Floriano
- */
- public class EditorDeTexto extends javax.swing.JFrame {
- /** Creates new form Editor */
- public EditorDeTexto() {
- initComponents();
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jMenuBar1 = new javax.swing.JMenuBar();
- jMenu1 = new javax.swing.JMenu();
- jMenuItem1 = new javax.swing.JMenuItem();
- jMenuItem2 = new javax.swing.JMenuItem();
- jMenu2 = new javax.swing.JMenu();
- jMenuItem3 = new javax.swing.JMenuItem();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jTextArea1.setColumns(20);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
- );
- jMenu1.setText("Options");
- jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
- jMenuItem1.setText("Save");
- jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem1ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem1);
- jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
- jMenuItem2.setText("Open");
- jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem2ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem2);
- jMenuBar1.add(jMenu1);
- jMenu2.setText("Help");
- jMenuItem3.setText("Report a bug");
- jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem3ActionPerformed(evt);
- }
- });
- jMenu2.add(jMenuItem3);
- jMenuBar1.add(jMenu2);
- setJMenuBar(jMenuBar1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
- try {
- Save();
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "FileNotFoundException");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, "IOException"); }
- }//GEN-LAST:event_jMenuItem1ActionPerformed
- private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
- try {
- Open();
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "FileNotFoundException");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, "IOException");
- }
- }//GEN-LAST:event_jMenuItem2ActionPerformed
- private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
- }//GEN-LAST:event_jMenuItem3ActionPerformed
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new EditorDeTexto().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JMenu jMenu1;
- private javax.swing.JMenu jMenu2;
- private javax.swing.JMenuBar jMenuBar1;
- private javax.swing.JMenuItem jMenuItem1;
- private javax.swing.JMenuItem jMenuItem2;
- private javax.swing.JMenuItem jMenuItem3;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JScrollPane jScrollPane1;
- static javax.swing.JTextArea jTextArea1;
- // End of variables declaration//GEN-END:variables
- public static void Save () throws FileNotFoundException, IOException {
- JFileChooser chooser = new JFileChooser();
- //Abre o file chooser. Ele retorna JFileChooser.APPROVE_OPTION caso o usuário
- //pressione ok.
- if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
- return; //Se não pressionou ok, saímos do método.
- }
- //Usamos o getSelectedFile() para pegar o nome do arquivo selecionado
- PrintWriter pw = new PrintWriter (chooser.getSelectedFile());
- pw.println (jTextArea1.getText());
- pw.flush();
- pw.close();
- }
- public static void Open () throws FileNotFoundException, IOException {
- JFileChooser loadEmp = new JFileChooser();//new dialog
- //Abre o file chooser. Ele retorna JFileChooser.APPROVE_OPTION caso o usuário
- //pressione ok.
- if (loadEmp.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
- return; //Se não pressionou ok, saímos do método.
- }
- FileReader fr = new FileReader(loadEmp.getSelectedFile());
- BufferedReader br = new BufferedReader(fr);
- String linha;
- StringBuilder sb = new StringBuilder();
- while((linha = br.readLine()) != null) {
- sb.append(linha).append("\n");
- }
- fr.close();
- jTextArea1.setText(sb.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment