Membuat Aplikasi Notepad Sederhana menggunakan Java

notePad

 
import java.awt.Component;
import java.io.*;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

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

 

/**
*
* @author Haidi
*/
public class Notepad2 extends javax.swing.JFrame {
private File file;
private boolean isSave=true;
private String bag_file;
private boolean fr;
/**
* Creates new form Notepad2
*/
public Notepad2() {
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();
TextArea = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
New = new javax.swing.JMenuItem();
Open = new javax.swing.JMenuItem();
Save = new javax.swing.JMenuItem();
SaveAs = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

TextArea.setBackground(new java.awt.Color(204, 204, 0));
TextArea.setColumns(20);
TextArea.setRows(5);
jScrollPane1.setViewportView(TextArea);

jMenu1.setBackground(new java.awt.Color(51, 0, 0));
jMenu1.setText(“File”);
jMenu1.setFont(new java.awt.Font(“Arial”, 0, 14)); // NOI18N
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});

New.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
New.setText(“New”);
New.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewActionPerformed(evt);
}
});
jMenu1.add(New);

Open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
Open.setText(“Open”);
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
jMenu1.add(Open);

Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
Save.setText(“Save”);
Save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveActionPerformed(evt);
}
});
jMenu1.add(Save);

SaveAs.setText(“SaveAs”);
SaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveAsActionPerformed(evt);
}
});
jMenu1.add(SaveAs);

Exit.setText(“Exit”);
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
jMenu1.add(Exit);

jMenuBar1.add(jMenu1);

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 NewActionPerformed(java.awt.event.ActionEvent evt) {
TextArea.setText(“”);
setTitle(“Notepad Haidi”);

}

private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}

private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(open);
if (option == JFileChooser.APPROVE_OPTION) {
TextArea.setText(“”);
try {
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) {
TextArea.append(scan.nextLine() + ” “);
}
fr = true;
bag_file = open.getSelectedFile().getPath();
} catch (Exception ex) {
System.out.println(ex.getMessage());

}
} // TODO add your handling code here:
}

private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}

private void SaveActionPerformed(java.awt.event.ActionEvent evt) {
if(file==null){
SaveAs();
}
else{
Save();
}

// TODO add your handling code here:
}

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate=”collapsed” desc=” Look and feel setting code (optional) “>
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Notepad2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Notepad2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Notepad2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Notepad2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Notepad2().setVisible(true);
}
});
}
// Variables declaration – do not modify
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem New;
private javax.swing.JMenuItem Open;
private javax.swing.JMenuItem Save;
private javax.swing.JMenuItem SaveAs;
private javax.swing.JTextArea TextArea;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration

private void SaveAs() {
JFileChooser fileChooser=new JFileChooser(“.”);
int choice=fileChooser.showSaveDialog(this);
if(choice==JFileChooser.APPROVE_OPTION){
file = new File(fileChooser.getSelectedFile().getPath()+”.txt”);
setTitle(file.getPath());
Save();
}
}
private void Save() {
if(file!=null){
FileWriter hd=null;
try {
hd=new FileWriter(file);
hd.write(TextArea.getText());
isSave=true;
}catch (Exception ex){
JOptionPane.showMessageDialog(this,”File Tidak Tersimpan”);
}
finally{
try{
hd.close();
}catch (Exception ex){
}
}
}
}
}

Terimakasih dan semoga bermanfaat dan jika ada kesalahan mohon di maafkan.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s