Tugas J2se Membuat Notepad

1. Deskripsi

Sebelumnya pasti paham dengan keberadaan notepad pada windows, dari situ saya mendapatkan tugas dari kampus membuat notepad yang dapat menyimpan, penghapus, dan membuka file. Langsung saja disini saya menggunakan pemrograman J2se.

2. Flowchart

notepad

3. Source Code

package notepad;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


/**
 *
 * @author Much_Saifurrachman
 */
public class Notepad extends JFrame{
    
     //membuat menu items
    private JMenuItem jmiOpen = new JMenuItem("Open");
    private JMenuItem jmiSave = new JMenuItem("Save");
    private JMenuItem jmiClear = new JMenuItem("Clear");
    private JMenuItem jmiExit = new JMenuItem("Exit");
  

    //membuat tombol pada tool bar
    private JLabel jlblStatus = new JLabel();


    //membuat JFileChooser
    private JFileChooser jFileChooser1 = new JFileChooser(new File("*.txt"));

    //membuat a text area
    private JTextArea jta = new JTextArea();

    public Notepad(){
    //menambah item menu pada menu
        JMenu jMenu1 = new JMenu("File");
        jMenu1.add(jmiOpen);
        jMenu1.add(jmiSave);
        jMenu1.add(jmiClear);
        jMenu1.addSeparator();
        jMenu1.add(jmiExit);
      

    //menambbah menu pada menu bar
        JMenuBar jMenuBar1 = new JMenuBar();
        jMenuBar1.add(jMenu1);
      

    //menyeting pada menu bar
        setJMenuBar(jMenuBar1);

  


        jmiOpen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            open();
        }
        });


        jmiSave.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            save();
        }
        });

        jmiClear.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            jta.setText(null);
        }
        });

        jmiExit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            System.exit(0);
        }
        });
        
        add(jlblStatus, BorderLayout.SOUTH);
        add(new JScrollPane(jta), BorderLayout.CENTER);
    }

    /** membuka file */
    private void open(){
        if (jFileChooser1.showOpenDialog(jmiOpen) == JFileChooser.APPROVE_OPTION)
            open(jFileChooser1.getSelectedFile());
    }
    private void open(File file){
        try{
            // membaca spesifikasi file dan memasukan ke jta
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file) );
            byte[] b = new byte[in.available()];
            in.read(b, 0, b.length);
            jta.append(new String(b, 0, b.length));
            in.close();

            //menampilkan status open file
            jlblStatus.setText(file.getName() + "Openend");

        }catch (IOException ex){
            jlblStatus.setText("Salah Mas Bro" + file.getName());

        }
    }

    //menyimpan file
private void save(){
if (jFileChooser1.showSaveDialog(jmiSave) ==
        JFileChooser.APPROVE_OPTION){
    save(jFileChooser1.getSelectedFile());
}
}

//menyimpan file instance
private void save(File file){
    try{
    //menulis text
        BufferedOutputStream out = new BufferedOutputStream(
                new FileOutputStream(file));
        byte[] b = (jta.getText()).getBytes();
        out.write(b, 0, b.length);
        out.close();

        // nemapilkan status
        jlblStatus.setText(file.getName() + "Saved");
    }catch (IOException ex){
       jlblStatus.setText("salah mbak bro"+ file.getName());
    }
}

Video Penjelasan

Advertisements

Posted on January 20, 2014, in JAVA. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: