- Back to Home »
- Java , Netbeans »
- Tutorial Aplikasi Notepad Menggunakan Java Netbeans
Posted by : Unknown
Sunday, January 19, 2014
Assalamualaikum. Wr.Wb....
hai blogerrs bertemu lagi dengan saya...
Kali ini saya akan bebrbagi bagaimana membuat notepad menggunakan Java Netbeans...
Ngak se selit yang dibayangkankan kok...
ok ngak perlu panjang lebar mari kita buat Notepad nya:
buatlah sebuah project baru dengan cara :
- klik file
- New priject
- Lalu ikutilah langkah-langkah selanjutnya..
- Lalu buatlah sebuah form baru dengan cara klik kalan di package lalu klik new
Letakkan beberapa komponen:
Icon Name Name Jumlah Text
--------------------------------------------------------------------------------------
1. Jpanel jPanel1 1 kosong
2. Jscroll Pane jScrollPan1 1 kosong
3. JTex Area jTextArea1 1 kosong
4. JMenu Bar jMenuBar1 2 File,Help
5. JMenu Item New 1 New
6. Jmenu Item Save 1 Save
7. Jmenu Item SaveAs 1 Save As
8. Jmenu Item Open 1 Open
9. Jmenu Item Save 1 Exit
10. Jmenu Item Help 1 Help
Susunlah seperti tampilan di bawah
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package notepad;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author Andri
*/
public class Notepad extends javax.swing.JFrame {
String filename;
String Tampil, Lokasi;
private File File;
private boolean Saved = true;
/**
* Creates new form Notepad
*
*/
public Notepad() {
initComponents();
setIconImage(new ImageIcon(getClass().getResource("icon.png")).getImage());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
/**
* 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() {
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();
New = new javax.swing.JMenuItem();
Save = new javax.swing.JMenuItem();
SaveAs = new javax.swing.JMenuItem();
Open = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
Help = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Notepad");
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, 481, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
);
jMenu1.setText("File");
New.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
New.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/new.png"))); // NOI18N
New.setText("New");
New.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewActionPerformed(evt);
}
});
jMenu1.add(New);
Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/Save-icon.png"))); // NOI18N
Save.setText("Save");
Save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveActionPerformed(evt);
}
});
jMenu1.add(Save);
SaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
SaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/filesaveas.png"))); // NOI18N
SaveAs.setText("Save As");
SaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveAsActionPerformed(evt);
}
});
jMenu1.add(SaveAs);
Open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
Open.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/open.png"))); // NOI18N
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
jMenu1.add(Open);
Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/exit.png"))); // NOI18N
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);
jMenu2.setText("Help");
Help.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/about.png"))); // NOI18N
Help.setText("About");
Help.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HelpActionPerformed(evt);
}
});
jMenu2.add(Help);
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>
private void SaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
save();
}
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
open();
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
exit();
}
private void HelpActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
About aba = new About();
aba.show();
}
private void NewActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
newFile();
}
private void SaveAsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
saveAs();
}
/**
* @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(Notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Notepad.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 Notepad().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem Help;
private javax.swing.JMenuItem New;
private javax.swing.JMenuItem Open;
private javax.swing.JMenuItem Save;
private javax.swing.JMenuItem SaveAs;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
public void newFile() {
if (jTextArea1.getText().equals("") || jTextArea1.getText().equals(Tampil)) {
jTextArea1.setText("");
Tampil = "";
Lokasi = "";
setTitle("Notepad ");
} else {
int i = JOptionPane.showConfirmDialog(null, "Isi file telah perubah!\nApakah anda ingin menyimpan perubahan?");
if (i == 0) {
save();
jTextArea1.setText("");
Lokasi = "";
setTitle("Notepad");
} else if (i == 1) {
jTextArea1.setText("");
Lokasi = "";
setTitle("Notepad");
} else if (i == 2) {
return;
}
}
}
public void open() {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int i = fc.showOpenDialog(this);
if (i == fc.CANCEL_OPTION) {
return;
}
File BukaFile = fc.getSelectedFile();
if (BukaFile == null || BukaFile.getName().equals("")) {
JOptionPane.showMessageDialog(this, "Pilih sebuah file!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
BufferedReader br = new BufferedReader(new FileReader(BukaFile));
StringBuffer sb = new StringBuffer();
String Baris;
while ((Baris = br.readLine()) != null) {
sb.append(Baris + "\n");
}
jTextArea1.setText(sb.toString());
Tampil = jTextArea1.getText();
Lokasi = BukaFile.toString();
setTitle(BukaFile.getName() + "- Notepad");
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "File tidak ditemukan:" + ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "IO Error:" + ex);
}
}
public void save() {
if (File != null) {
FileWriter filewriter = null;
try {
filewriter = new FileWriter(File);
filewriter.write(jTextArea1.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 {
saveAs();
}
}
public void saveAs() {
JFileChooser fc = new JFileChooser(".");
int i = fc.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File = new File(fc.getSelectedFile().getPath() + ".txt");
setTitle(File.getPath());
save();
}
}
public void exit() {
if (jTextArea1.getText().equals("") || jTextArea1.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) {
saveAs();
} else if (i == 1) {
System.exit(0);
} else if (i == 2) {
return;
}
}
}
}
Link Youtube http://youtu.be/ZjZmyBzDTnU
Ok demikian tutorial kali semoga bermanfaat...
bagi teman bolers kritik dan saran sangat saya butuhkan untuk memperbaiki dalam pembuatan tutorial selanjutnya...
Wasalamualaikum Wr.Wb.
SALAM ITB !!!!
hai blogerrs bertemu lagi dengan saya...
Kali ini saya akan bebrbagi bagaimana membuat notepad menggunakan Java Netbeans...
Ngak se selit yang dibayangkankan kok...
ok ngak perlu panjang lebar mari kita buat Notepad nya:
buatlah sebuah project baru dengan cara :
- klik file
- New priject
- Lalu ikutilah langkah-langkah selanjutnya..
- Lalu buatlah sebuah form baru dengan cara klik kalan di package lalu klik new
Gambar 1.1
Letakkan beberapa komponen:
Icon Name Name Jumlah Text
--------------------------------------------------------------------------------------
1. Jpanel jPanel1 1 kosong
2. Jscroll Pane jScrollPan1 1 kosong
3. JTex Area jTextArea1 1 kosong
4. JMenu Bar jMenuBar1 2 File,Help
5. JMenu Item New 1 New
6. Jmenu Item Save 1 Save
7. Jmenu Item SaveAs 1 Save As
8. Jmenu Item Open 1 Open
9. Jmenu Item Save 1 Exit
10. Jmenu Item Help 1 Help
Susunlah seperti tampilan di bawah
Gambar 2.1
Berikut kode lengkapnya:/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package notepad;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author Andri
*/
public class Notepad extends javax.swing.JFrame {
String filename;
String Tampil, Lokasi;
private File File;
private boolean Saved = true;
/**
* Creates new form Notepad
*
*/
public Notepad() {
initComponents();
setIconImage(new ImageIcon(getClass().getResource("icon.png")).getImage());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
/**
* 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() {
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();
New = new javax.swing.JMenuItem();
Save = new javax.swing.JMenuItem();
SaveAs = new javax.swing.JMenuItem();
Open = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
Help = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Notepad");
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, 481, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
);
jMenu1.setText("File");
New.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
New.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/new.png"))); // NOI18N
New.setText("New");
New.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewActionPerformed(evt);
}
});
jMenu1.add(New);
Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/Save-icon.png"))); // NOI18N
Save.setText("Save");
Save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveActionPerformed(evt);
}
});
jMenu1.add(Save);
SaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
SaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/filesaveas.png"))); // NOI18N
SaveAs.setText("Save As");
SaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveAsActionPerformed(evt);
}
});
jMenu1.add(SaveAs);
Open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
Open.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/open.png"))); // NOI18N
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
jMenu1.add(Open);
Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/exit.png"))); // NOI18N
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);
jMenu2.setText("Help");
Help.setIcon(new javax.swing.ImageIcon(getClass().getResource("/notepad/about.png"))); // NOI18N
Help.setText("About");
Help.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HelpActionPerformed(evt);
}
});
jMenu2.add(Help);
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>
private void SaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
save();
}
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
open();
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
exit();
}
private void HelpActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
About aba = new About();
aba.show();
}
private void NewActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
newFile();
}
private void SaveAsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
saveAs();
}
/**
* @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(Notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Notepad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Notepad.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 Notepad().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem Help;
private javax.swing.JMenuItem New;
private javax.swing.JMenuItem Open;
private javax.swing.JMenuItem Save;
private javax.swing.JMenuItem SaveAs;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
public void newFile() {
if (jTextArea1.getText().equals("") || jTextArea1.getText().equals(Tampil)) {
jTextArea1.setText("");
Tampil = "";
Lokasi = "";
setTitle("Notepad ");
} else {
int i = JOptionPane.showConfirmDialog(null, "Isi file telah perubah!\nApakah anda ingin menyimpan perubahan?");
if (i == 0) {
save();
jTextArea1.setText("");
Lokasi = "";
setTitle("Notepad");
} else if (i == 1) {
jTextArea1.setText("");
Lokasi = "";
setTitle("Notepad");
} else if (i == 2) {
return;
}
}
}
public void open() {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int i = fc.showOpenDialog(this);
if (i == fc.CANCEL_OPTION) {
return;
}
File BukaFile = fc.getSelectedFile();
if (BukaFile == null || BukaFile.getName().equals("")) {
JOptionPane.showMessageDialog(this, "Pilih sebuah file!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
BufferedReader br = new BufferedReader(new FileReader(BukaFile));
StringBuffer sb = new StringBuffer();
String Baris;
while ((Baris = br.readLine()) != null) {
sb.append(Baris + "\n");
}
jTextArea1.setText(sb.toString());
Tampil = jTextArea1.getText();
Lokasi = BukaFile.toString();
setTitle(BukaFile.getName() + "- Notepad");
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "File tidak ditemukan:" + ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "IO Error:" + ex);
}
}
public void save() {
if (File != null) {
FileWriter filewriter = null;
try {
filewriter = new FileWriter(File);
filewriter.write(jTextArea1.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 {
saveAs();
}
}
public void saveAs() {
JFileChooser fc = new JFileChooser(".");
int i = fc.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File = new File(fc.getSelectedFile().getPath() + ".txt");
setTitle(File.getPath());
save();
}
}
public void exit() {
if (jTextArea1.getText().equals("") || jTextArea1.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) {
saveAs();
} else if (i == 1) {
System.exit(0);
} else if (i == 2) {
return;
}
}
}
}
Link Youtube http://youtu.be/ZjZmyBzDTnU
Ok demikian tutorial kali semoga bermanfaat...
bagi teman bolers kritik dan saran sangat saya butuhkan untuk memperbaiki dalam pembuatan tutorial selanjutnya...
Wasalamualaikum Wr.Wb.
SALAM ITB !!!!