Wednesday, January 2, 2013

Tutorial Pemograman Java Netbean Membuat Aplikasi Notepad Sederhana



Nama    : Ersus Saeful Hidayat
NIM      : 49012050
Jurusan  : TKJMD – STEI - ITB





Link Video Youtube klik disini

Sourcecode :


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Notepad.java
 *
 * Created on Jan 2, 2013, 2:09:49 PM
 */

/**
 * ============================================================================
 * @author MALIKA AZZAHRA
 * ============================================================================
 * Nama     : Ersus Saeful Hidayat
 * NIM      : 49012050
 * Jurusan  : TKJMD (Teknik Komputer Jaringan Media Digital) STEI - ITB
 * ============================================================================
 */
public class Notepad extends javax.swing.JFrame {
    String Tampil, Lokasi;
    private File File;
    private boolean Saved = true;
    /** Creates new form Notepad */
    public Notepad() {
        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">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemNew = new javax.swing.JMenuItem();
        jMenuItemOpen = new javax.swing.JMenuItem();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenuItemSaveAs = new javax.swing.JMenuItem();
        jMenuItemKeluar = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea.setColumns(20);
        jTextArea.setRows(5);
        jScrollPane1.setViewportView(jTextArea);

        jMenu1.setText("File");

        jMenuItemNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemNew.setIcon(new javax.swing.ImageIcon("D:\\Documents\\Data Campus\\ITB SEAMOLEC\\Kuliah Tahap II - ITB B6 - TKJMD\\Course LSKK Website\\TMD11 Pemrograman Java2SE\\Icon\\page.gif")); // NOI18N
        jMenuItemNew.setText("New");
        jMenuItemNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemNew);

        jMenuItemOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemOpen.setIcon(new javax.swing.ImageIcon("D:\\Documents\\Data Campus\\ITB SEAMOLEC\\Kuliah Tahap II - ITB B6 - TKJMD\\Course LSKK Website\\TMD11 Pemrograman Java2SE\\Icon\\folder_open.gif")); // NOI18N
        jMenuItemOpen.setText("Open");
        jMenuItemOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemOpenActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemOpen);

        jMenuItemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemSave.setIcon(new javax.swing.ImageIcon("D:\\Documents\\Data Campus\\ITB SEAMOLEC\\Kuliah Tahap II - ITB B6 - TKJMD\\Course LSKK Website\\TMD11 Pemrograman Java2SE\\Icon\\disk.gif")); // NOI18N
        jMenuItemSave.setText("Save");
        jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemSave);

        jMenuItemSaveAs.setIcon(new javax.swing.ImageIcon("D:\\Documents\\Data Campus\\ITB SEAMOLEC\\Kuliah Tahap II - ITB B6 - TKJMD\\Course LSKK Website\\TMD11 Pemrograman Java2SE\\Icon\\savenote.gif")); // NOI18N
        jMenuItemSaveAs.setText("SaveAs..");
        jMenuItemSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveAsActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemSaveAs);

        jMenuItemKeluar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemKeluar.setIcon(new javax.swing.ImageIcon("D:\\Documents\\Data Campus\\ITB SEAMOLEC\\Kuliah Tahap II - ITB B6 - TKJMD\\Course LSKK Website\\TMD11 Pemrograman Java2SE\\Icon\\home.gif")); // NOI18N
        jMenuItemKeluar.setText("Exit");
        jMenuItemKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemKeluarActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemKeluar);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        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(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                       

    private void jMenuItemNewActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        File_baru();
    }                                           

    private void jMenuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        Buka_File();
    }                                            

    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        if (File == null)
        {
            SimpanAs_File();
        }
        else
        {
            Simpan_File();
        }
    }                                            

    private void jMenuItemSaveAsActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        SimpanAs_File();
    }                                              

    private void jMenuItemKeluarActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        Keluar();
    }                                              

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Notepad().setVisible(true);
            }
        });
    }

    public void File_baru()
    {
        if (jTextArea.getText().equals("") || jTextArea.getText().equals(Tampil))
        {
            jTextArea.setText("");
            Tampil = "";
            Lokasi = "";
            setTitle("Belum ada judul file - Notepad Saeful");
        }
        else
        {
            int i = JOptionPane.showConfirmDialog(null,"Isi file telah perubah!\nApakah anda ingin menyimpan perubahan?");
            if (i == 0)
                Simpan_File();
            else if (i == 1)
            {
                jTextArea.setText("");
                Lokasi = "";
                setTitle("Belum ada judul file - Notepad Saeful");
            }
            else if (i == 2)
                return;
        }
    }
   
    public void Buka_File()
    {
        JFileChooser filechooser = new JFileChooser();
        filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int i = filechooser.showOpenDialog(this);
        if (i == filechooser.CANCEL_OPTION)
            return;
        File BukaFile = filechooser.getSelectedFile();
        if (BukaFile == null || BukaFile.getName().equals(""))
        {
            JOptionPane.showMessageDialog(this, "Pilih sebuah file!", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            BufferedReader bufferedreader = new BufferedReader(new FileReader(BukaFile));
            StringBuffer stringbuffer = new StringBuffer();
            String Baris;
            while((Baris = bufferedreader.readLine()) != null)stringbuffer.append(Baris+"\n");
            jTextArea.setText(stringbuffer.toString());
            Tampil = jTextArea.getText();
            Lokasi = BukaFile.toString();
            setTitle(BukaFile.getName()+"- Notepad Saeful");
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "File tidak ditemukan:"+ ex);
        } catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, "IO Error:"+ ex);
        }
    }

    public void Simpan_File()
    {
        if (File != null)
        {
            FileWriter filewriter = null;
            try {
                filewriter = new FileWriter(File);
                filewriter.write(jTextArea.getText());
                filewriter.flush();
                Saved = true;
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Terjadi kesalahan!\nTidak dapat disimpan");
            } finally
            {
                try {
                    filewriter.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        else
        {
            SimpanAs_File();
        }
    }

    public void SimpanAs_File()
    {
        JFileChooser filechooser = new JFileChooser(".");
        int i = filechooser.showSaveDialog(this);
        if (i == JFileChooser.APPROVE_OPTION)
        {
            File = new File(filechooser.getSelectedFile().getPath()+".txt");
            setTitle(File.getPath());
            Simpan_File();
        }
    }

    public void Keluar()
    {
        if (jTextArea.getText().equals("") || jTextArea.getText().equals(Tampil))
            System.exit(0);
        else
        {
            int i = JOptionPane.showConfirmDialog(null, "Isi file belum disimpan atau ada perubahan!\nApakah anda ingin menyimpan perubahan?");
            if (i == 0)
                Simpan_File();
            else if (i == 1)
                System.exit(0);
            else if (i == 2)
                return;
        }
    }

    // Variables declaration - do not modify                    
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemKeluar;
    private javax.swing.JMenuItem jMenuItemNew;
    private javax.swing.JMenuItem jMenuItemOpen;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JMenuItem jMenuItemSaveAs;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea;
    // End of variables declaration                  

}

Terimakasih, Semoga bermanfaat.

No comments:

Post a Comment