Rabu, 14 Maret 2012

Stream I/O TUGAS 1


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

/*
 * ProjectIO.java
 *
 * Created on Feb 9, 2012, 6:47:33 PM
 */
package project_io;

/**
 *
 * @author rachmad
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.filechooser.*;
import javax.swing.*;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.swing.filechooser.FileFilter;

public class ProjectIO extends javax.swing.JFrame {
    JFileChooser fc;
    File file;
    boolean Nfile;
    final String[] EXT = { ".csv" };
    final String[] EXT2 = { ".txt", ".java" };
    private String text;
   
    public ProjectIO() {
        initComponents();
        editor.setVisible(false);
        editor2.setFocusable(false);
        editor2.setEditable(false);
        this.setTitle("Untitled - NOTES");
        fc = new JFileChooser();
        fc.addChoosableFileFilter(new jenisFile(EXT));
        fc.addChoosableFileFilter(new jenisFile(EXT2));
        fc.setAcceptAllFileFilterUsed(false);
        Nfile = true;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        btnNew = new javax.swing.JButton();
        btnOpen = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnSaveAs = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtnamafield = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        txtPass = new javax.swing.JTextField();
        btnClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        editor = new javax.swing.JEditorPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        txtCPass = new javax.swing.JTextField();
        btnDes = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        btnCrypt = new javax.swing.JButton();
        btnMD5 = new javax.swing.JButton();
        btnRC = new javax.swing.JButton();
        btnCC = new javax.swing.JButton();
        spnGeser = new javax.swing.JSpinner();
        btnSave2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        editor2 = new javax.swing.JEditorPane();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setFont(new java.awt.Font("Arial Black", 0, 10)); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204), 2), "First Text", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("BlockUp", 0, 14))); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 153, 204));
        jPanel1.setFont(new java.awt.Font("Autobahn", 0, 11)); // NOI18N

        jComboBox1.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Menu", "New", "Open", "Save", "Save As" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204)), "Menu", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("BlockUp", 0, 14))); // NOI18N
        jPanel3.setFont(new java.awt.Font("Sylfaen", 0, 11)); // NOI18N

        btnNew.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnOpen.setFont(new java.awt.Font("Book Antiqua", 0, 12));
        btnOpen.setText("Open");
        btnOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenActionPerformed(evt);
            }
        });

        btnSave.setFont(new java.awt.Font("Book Antiqua", 0, 12));
        btnSave.setText("Save");
        btnSave.setEnabled(false);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnSaveAs.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        btnSaveAs.setText("Save As");
        btnSaveAs.setEnabled(false);
        btnSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveAsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNew, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSaveAs, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnOpen, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNew)
                    .addComponent(btnOpen))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnSaveAs))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        jLabel1.setText("Nama File");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204)), "Password", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("BlockUp", 0, 14))); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtPass, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        btnClear.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        btnClear.setText("Clear Text");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        editor.setEnabled(false);
        jScrollPane1.setViewportView(editor);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(txtnamafield, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(jLabel1)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(108, 108, 108)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnClear)
                        .addGap(21, 21, 21))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtnamafield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnClear)
                .addGap(11, 11, 11)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204), 2), "Enkripsi Text", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("BlockUp", 0, 14))); // NOI18N
        jPanel2.setForeground(new java.awt.Color(153, 255, 204));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204)), "Password", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("BlockUp", 0, 14))); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCPass, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnDes.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        btnDes.setText("Description");
        btnDes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesActionPerformed(evt);
            }
        });

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204)), "Enkripsi", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("BlockUp", 0, 14))); // NOI18N

        btnCrypt.setFont(new java.awt.Font("Book Antiqua", 0, 12));
        btnCrypt.setText("Cryptography");
        btnCrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCryptActionPerformed(evt);
            }
        });

        btnMD5.setFont(new java.awt.Font("Book Antiqua", 0, 12));
        btnMD5.setText("MD5");
        btnMD5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMD5ActionPerformed(evt);
            }
        });

        btnRC.setFont(new java.awt.Font("Book Antiqua", 0, 12));
        btnRC.setText("Random Char");
        btnRC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRCActionPerformed(evt);
            }
        });

        btnCC.setFont(new java.awt.Font("Book Antiqua", 0, 12));
        btnCC.setText("Caesar Chiper");
        btnCC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCCActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnRC, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCrypt, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnMD5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCrypt)
                    .addComponent(btnMD5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRC)
                    .addComponent(btnCC))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnSave2.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        btnSave2.setText("Save");
        btnSave2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSave2ActionPerformed(evt);
            }
        });

        editor2.setEnabled(false);
        jScrollPane2.setViewportView(editor2);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(spnGeser, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnSave2)
                        .addGap(96, 96, 96)
                        .addComponent(btnDes, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                        .addGap(31, 31, 31))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(spnGeser, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDes)
                    .addComponent(btnSave2))
                .addGap(29, 29, 29)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        btnExit.setFont(new java.awt.Font("Book Antiqua", 0, 12)); // NOI18N
        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(28, 28, 28)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(btnExit)
                .addContainerGap(397, Short.MAX_VALUE))
        );

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

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {                                      
        System.exit(0);
    }                                      

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {                                      
        editor.setText("");
        btnSave.setEnabled(true);
        btnSaveAs.setEnabled(true);
        editor.requestFocus();
        this.setTitle("Untitled - NOTES");
        Nfile = true;
        editor.setEnabled(true);
        editor.setVisible(true);
    }                                    

    private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {                                      
        int hasil = fc.showOpenDialog(this);
        editor.setEnabled(true);
        editor.setVisible(true);
        btnSave.setEnabled(true);
        btnSaveAs.setEnabled(true);
        if (hasil == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        try {
        editor.read(new FileInputStream(file), null);
        } catch (IOException e) {
        msg("Gagal Buka File : "+file.getName() );
        return ;
        }
        }
        this.setTitle(file.getName()+" - NOTES" );
        Nfile = false;
        text=file.getPath().toString();
        txtnamafield.setText(text);
    }                                      

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                      
        if (Nfile)
        btnSaveAs.doClick();
        else
        SimpanFile();
    }                                      

    private void btnSaveAsActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
            file = fc.getSelectedFile();
            if (!file.exists() || okToReplace())
            SimpanFile();
        }
    }                                        

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                        
        editor.setText("");
    }                                      

    private void btnSave2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
        file = fc.getSelectedFile();
        if (!file.exists() || okToReplace())
        SimpanFile2();
        }
    }                                      

    private void btnDesActionPerformed(java.awt.event.ActionEvent evt) {                                      
            if (txtCPass.getText().equals(txtPass.getText())){
                JOptionPane.showMessageDialog(null, "Descryption Success");
                editor2.setText(editor.getText());
            }
            else
            JOptionPane.showMessageDialog(null, "What Are You Doing");
    }                                    

    private void btnCryptActionPerformed(java.awt.event.ActionEvent evt) {                                        
        encrypt();
    }                                      

    private void btnRCActionPerformed(java.awt.event.ActionEvent evt) {                                    
        String randomKey = "";
        double randomNumber;
        for (int i=0; i<editor.getText().length(); i++) {
             randomNumber = Math.random() * 35 + (int)spnGeser.getValue();
             randomKey = randomKey + (char)randomNumber;
        }
        editor2.setText(randomKey);
    }                                    

    private void btnMD5ActionPerformed(java.awt.event.ActionEvent evt) {                                      
        EncryptMD5 md5=new EncryptMD5();
        try {
            editor2.setText(md5.MD5(editor.getText()));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ProjectIO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ProjectIO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                    

    private void btnCCActionPerformed(java.awt.event.ActionEvent evt) {                                    
    getData=editor.getText();
    getData=getData.trim();
    len=getData.length();
    con=getData.toCharArray();
    j=len+1;
    getData="";
    try {
    for( i1=0;i1<j;i1++){  
          a=con[i1];
          k=(int)a;
          k+=(int)spnGeser.getValue();
          a=(char)k;
       getData=getData+(a.toString());
    } }
    catch(Exception e){

    }
    System.out.println(getData);
    System.out.println(len);
    editor2.setText(getData);
    }                                    

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        if(jComboBox1.getSelectedItem().equals("New")){
            editor.setText("");
            btnSave.setEnabled(true);
            btnSaveAs.setEnabled(true);
            editor.requestFocus();
            this.setTitle("Untitled - NOTES");
            Nfile = true;
            editor.setEnabled(true);
            editor.setVisible(true);
        }
        if(jComboBox1.getSelectedItem().equals("Open")){
            int hasil = fc.showOpenDialog(this);
            editor.setEnabled(true);
            editor.setVisible(true);
            btnSave.setEnabled(true);
            btnSaveAs.setEnabled(true);
            if (hasil == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            try {
            editor.read(new FileInputStream(file), null);
            } catch (IOException e) {
            msg("Gagal Buka File : "+file.getName() );
            return ;
            }
            }
            this.setTitle(file.getName()+" - NOTES" );
            Nfile = false;
        }
        if(jComboBox1.getSelectedItem().equals("Save")){
            if (Nfile)
            btnSaveAs.doClick();
            else
            SimpanFile();
        }
        if(jComboBox1.getSelectedItem().equals("Save As")){
            if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
            file = fc.getSelectedFile();
            if (!file.exists() || okToReplace())
            SimpanFile();
            }
        }
    }                                        
   private void msg(String pesan) {
    JOptionPane.showMessageDialog(this,pesan,"Informasi",
    JOptionPane.INFORMATION_MESSAGE);
    }
   
    private boolean okToReplace() {
    return (JOptionPane.showConfirmDialog(this,
    "File "+file.getName()+" Sudah Ada\n"+
    "Akan Ditimpa ?",
    "Peringatan",JOptionPane.YES_NO_OPTION)==0) ;
    }
   
    private void SimpanFile2() {
    PrintWriter pw = null;
    try {
    pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    } catch (IOException e) {
    msg("Gagal Simpan File : '" + file.getName());
    return;
    }
    pw.print(editor2.getText());
    pw.close();
    this.setTitle(file.getName()+" - NOTES" );
    Nfile = false;
    }
   
   
    private void SimpanFile() {
    PrintWriter pw = null;
    try {
    pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    } catch (IOException e) {
    msg("Gagal Simpan File : '" + file.getName());
    return;
    }
    pw.print(editor.getText());
    pw.close();
    this.setTitle(file.getName()+" - NOTES" );
    Nfile = false;
    }
   
    String getData,cs,main="";
    char con[];
    int len,hc,j=0;
    int i1=0;
    char bb[];
    int k=0;
    Character a;
 
   
    public void encrypt() {
    getData=editor.getText();
    getData=getData.trim();
    len=getData.length();
    con=getData.toCharArray();
    j=len-1;
    getData="";
    try {
    for( i1=j;i1>=0;i1--){  
          a=con[i1];
          k=(int)a;
          k+=(int)spnGeser.getValue();
          a=(char)k;
       getData=getData+(a.toString());
    } }
    catch(Exception e){

    }
    System.out.println(getData);
    System.out.println(len);
    editor2.setText(getData);
    }
   
   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ProjectIO().setVisible(true);
            }
        });
    }
   
    class EncryptMD5 {

    private String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - (int)spnGeser.getValue())));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }

    public  String MD5(String text)
    throws NoSuchAlgorithmException, UnsupportedEncodingException  {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }
   
   
   
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCC;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnCrypt;
    private javax.swing.JButton btnDes;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnMD5;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnOpen;
    private javax.swing.JButton btnRC;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSave2;
    private javax.swing.JButton btnSaveAs;
    private javax.swing.JEditorPane editor;
    private javax.swing.JEditorPane editor2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner spnGeser;
    private javax.swing.JTextField txtCPass;
    private javax.swing.JTextField txtPass;
    private javax.swing.JTextField txtnamafield;
    // End of variables declaration
}

NB : UNTUK PENGGUNAAN LEBIH LANJUT AKAN DILANJUTKAN PADA SESI BERIKUTNYA...... SELAMAT MENCOBA

GUI EVENT HANDLING

PENGERTIAN


Event Handling merupakan konsep penanganan suatu action yang terjadi. Jadi suatu program akan berjalan saat sesuatu terjadi, misalnya saat tombol diklik, saat combo box dipilih dan sebagainya. Java memiliki beberapa jenis Event Handling, salah satunya adalah class ActionListener yang menangani aksi terhadap tombol. 


KONSEP DASAR,PENERAPAN DAN CONTOH

Untuk  memahami  model,  pertama-tama  mari  kita  pelajari  melalui  tiga

komponen utamanya.


1. Event Source

   Event  source  mengacu  pada  komponen  GUI  yang  meng-generate  event.  Sebagai

contoh, jika user menekan tombol, event source dalam hal ini adalah tombol.

2. Event Listener/Handler

   Event  listener menerima  berita  dari  event-event  dan  proses-proses  interaksi  user.

   Ketika  tombol  ditekan,  listener  akan  mengendalikan  dengan  menampilkan  sebuah

informasi yang berguna untuk user.


3. Event Object

   Ketika sebuah event  terjadi (misal, ketika user berinteraksi dengan komponen GUI),

sebuah  object  event  diciptakan.  Object  berisi  semua  informasi  yang  perlu  tentang

event yang telah terjadi. Informasi meliputi tipe dari event yang telah terjadi, seperti

ketika mouse  telah di-klik. Ada beberapa  class event   untuk kategori  yang berbeda

dari user action. Sebuah event object mempunyai tipe data mengenai salah satu dari

class ini.


Class - class Event



Contoh program :
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame{
private JButton tombolKu = new JButton(“OK”),
tombol2 = new JButton(“Cancel”);
private JLabel labelKu = new JLabel(” << ANDA TELAH MEMILIH OK >> “),
label2 = new JLabel(” << ANDA TELAH MEMILIH CANCEL >> “);
private JPanel panelKu = new JPanel();
Main() {
super(“Membuat Tombol”);
panelKu.setLayout(null);
setSize(450, 140);
setDefaultCloseOperation(EXIT_ON_CLOSE);
labelKu.setBounds(110,20,450,25);
label2.setBounds(94,20,450,25);
labelKu.setVisible(false);
label2.setVisible(false);
tombolKu.setBounds(90,70,100,25);
tombol2.setBounds(250,70,100,25);
tombol2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evn){
label2.setVisible(true);
tombolKu.setEnabled(false);
}
});
tombolKu.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evn){
labelKu.setVisible(true);
tombol2.setEnabled(false);
}
});
panelKu.add(labelKu);
panelKu.add(tombolKu);
panelKu.add(tombol2);
panelKu.add(label2);
getContentPane().add(panelKu);
setVisible(true);
}
public static void main(String[] args) {
Main evenKu = new Main();
}
}


Sedikit penjelasan, pada program diatas saya menggunakan event Action dengan 2 kejadian, yaitu setEnabled(false) dan setVisible(true) saat menekan tombol OK atau cancel.

JAVA GUI


Pengertian Java GUI

Java GUI merupakan sebuah konsep yang memadukan dua buah teknologi yang ada yaitu Java dan GUI (Graphical User Interface). Seperti kita ketahui bersama Java merupakan bahasa pemrograman yang sudah mulai banyak digunakan saat ini karena beberapa kelebihan yang dimiliki seperti kemampuan multiplatform yang dimiliki, OOP, pustaka Class yang lengkap dsb.
Sejarah…
Adapun pada versi awal peluncurannya pada tahun 1996 (selanjutnya disebut versi 1.0), bahasa pemrograman ini menyertakan beberapa paket standar yakni :
1.  java.lang yakni sebuah kelas untuk elemen-elemen dasar
2.  java.io yang merupakan kelas input dan output termasuk penggunaan file
3.  java.util yakni kelas pelengkap seperti kelas struktur data dan kelas penanggalan
4.  java.net yakni kelas TCP/IP (untuk jaringan)
5.  java.awt yakni kelas dasar untuk aplikasi antarmuka dengan pengguna (GUI)
6.  java.applet yakni kelas dasar aplikasi antarmuka untuk browser
Sejak awal dibuatnya oleh Sun Microsystem, Java diorientasikan agar memiliki perangkat bergaya desktop dengan ToolkitJava yang dikenal dengan nama AWT (Abstract Windowing Toolkit). Kemudian pada tahun 1997 muncul JFC (Java Foundation Class) untuk kepentingan pembuatan aplikasi Window yang merupakan pengembangan dari AWT. JFC ini mempunyai kode swing yang terdapat pada package yang disediakan pada JAVA SDK versi 1.2 ke atas (Java 2).
Apa sebenarnya GUI…???????
Menurut referensi yang saya dapatkan, GUI (Graphical User Interface) merupakan sebuah teknologi antarmuka pengguna yang memungkinkan seseorang untuk berinteraksi dengan program dengan berbagai cara selain mengetik seperti halnya komputer, MP3 Player dll yang berisikan banyak tombol bergambar/icon dibandingkan teks. GUI yang merupakan kebalikan dari CLI (Command Line Interface) menggunakan icon grafis atau indikator visual untuk kenyamanan user.
Next, Java GUI….
Java sendiri menyediakan dua buah pustaka grafis dalam paketnya yaitu AWT dan Swing. Keduanya menyediakan komponen GUI yang dapat digunakan dalam membuat aplikasi Java dan Applet. Tidak seperti komponen AWT yang menggunakan native code, keseluruhan Swing menyediakan implementasi platform-independent dimana aplikasi yang dikembangkan dengan platform yang berbeda dapat memiliki tampilan yang sama.
Terdapat 4 hal yang perlu diperhatikan pada pemrograman GUI ini yaitu :
1.  Container (tempat) yaitu obyek yang dapat menampung komponen grafis dan tempat yang lebih kecil. Contoh : frames, panels.
2.  Component yaitu obyek yang berfungsi untuk menampilkan data, menerima masukan atau menunjukkan suatu kondisi. Contoh : buttons, labels dan text fields.
3.  Event yaitu obyek yang merepresentasikan suatu kejadian. Contoh gerakan mouse, penekanan tombol.
4.  Listener yaitu suatu obyek yang ‘menunggu’ suatu kejadian khusus dan akan bereaksi (melakukan sesuatu) jika kejadian tersebut terjadi.
sumber : http://aryavanjava.wordpress.com/category/java/ 


Konsep Dasar 

Java GUI merupakan sebuah konsep yang memadukan dua buah teknologi yang ada yaitu Java dan GUI (Graphical User Interface). Seperti kita ketahui bersama Java merupakan bahasa pemrograman yang sudah mulai banyak digunakan saat ini karena beberapa kelebihan yang dimiliki seperti kemampuan multiplatform yang dimiliki, OOP, pustaka Class yang lengkap dsb. 

Sejarah…
Adapun pada versi awal peluncurannya pada tahun 1996 (selanjutnya disebut versi 1.0), bahasa pemrograman ini menyertakan beberapa paket standar yakni :
1.   java.lang yakni sebuah kelas untuk elemen-elemen dasar
2.   java.io yang merupakan kelas input dan output termasuk penggunaan file
3.   java.util yakni kelas pelengkap seperti kelas struktur data dan kelas penanggalan
4.   java.net yakni kelas TCP/IP (untuk jaringan)
5.   java.awt yakni kelas dasar untuk aplikasi antarmuka dengan pengguna (GUI)
6.   java.applet yakni kelas dasar aplikasi antarmuka untuk browser

AWT

AWT merupakan singkatan dari Abstract Window Toolkit (meskipun orangdatang dengan memiliki banyak  ekspansi lainnya). AWT ini sekarang merupakan bagian dari Java Foundation Classes (JFC). API standar untuk menyediakan antarmuka pengguna grafis (GUI) untuk program Java. AWT juga toolkit GUI untukbeberapa profil Java ME. 

contoh penggunaan AWT

import java.awt.*;
public class FlowAp extends Frame{
public static void main(String argv[]){
            FlowAp fa=new FlowAp();
            //Change from BorderLayout default
            fa.setLayout(new FlowLayout());
            fa.setSize(400,300);
            fa.setVisible(true);
    }
    FlowAp(){
                add(new Button("SATU"));
                add(new Button("DUA"));
                add(new Button("TIGA"));
                add(new Button("EMPAT"));
                add(new Button("LIMA"));
                add(new Button("ENAM"));
                add(new Button("TUJUH"));
                add(new Button("DELAPAN"));
                add(new Button("SEMBILAN"));
                add(new Button("SEPULUH"));
        }//End of constructor
}//End of Application
 
SWING
 
 
Swing adalah bagian utama Java GUI widget toolkit.Swing adalah bagian dari Oracle's Java Foundation Classes (JFC)-API untuk menyediakan  graphical user interface (GUI) untuk program Java. Swing dikembangkan untuk menyediakan satu set yang lebih canggih dari komponen GUI dari Abstract Window Toolkit (AWT). 
 public static void main(String[] args) { SwingExample se = new SwingExample(); // Schedules theimport javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class SwingExample implements Runnable { @Overridepublic void run() { // Create the window JFrame f = new JFrame ("Hello, World!"); // Sets the behaviour for when the window is closedf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add a label and a buttonf.getContentPane().add(new JLabel("Hello, world!")); f.getContentPane().add(new JButton("Press me!")); // arrange the components inside the window f.pack(); //By default, the window is not visible. Make it visible. f.setVisible(true); } application to be run at the correct time in the event queue. SwingUtilities.invokeLater(se); } }
SWT
Standard Widget Toolkit (SWT) adalah toolkit widget grafis untuk digunakan dengan platform Java. Ini pada awalnya dikembangkan oleh IBM dan sekarangdikelola oleh  Eclipse Foundation in tandem with the Eclipse IDE. Ini adalah sebuah alternatif untuk Window Toolkit Abstrak (AWT) dan Swing Java GUI toolkitsdisediakan oleh Sun Microsystems sebagai bagian dari Java Platform, Standard Edition.
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
 
public class HelloWorld 
{
   public static void main (String[] args) 
  {
      Display display = new Display();
      Shell shell = new Shell(display);
      Label label = new Label(shell, SWT.NONE);
      label.setText("Hello World");
      label.pack();
      shell.pack();
      shell.open();
      while (!shell.isDisposed()) 
      {
         if (!display.readAndDispatch()) display.sleep();
      }
      display.dispose();
   }
}

ABOUT

selamat menikmati blog saya... semoga puass.. kritik dan saran sangat membantu

Slider(Do not Edit Here!)

Navigation

terima kasih atas kunjungan

Copyright Text