Important alert: (current site time 5/25/2013 1:16:36 PM EDT)
 

VB icon

NotePad

Email
Submitted on: 3/7/2012 5:27:34 PM
By: Arunkumar Palathera 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 2072
 
     simple notepad application.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: NotePad
// Description:simple notepad application.
// By: Arunkumar Palathera
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7006&lngWId=2//for details.//**************************************

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * NotePad.java
 *
 * Created on Nov 3, 2006, 9:00:51 AM
 */
/**
 *
 * @author ARUNKUMAR
 */
public class NotePad extends javax.swing.JFrame {
/** Creates new form NotePad */
File file;
String cp;
public NotePad() {
setTitle("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();
jTextArea1 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jMenuItem6 = new javax.swing.JMenuItem();
jMenuItem7 = new javax.swing.JMenuItem();
jMenuItem8 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem9 = new javax.swing.JMenuItem();
jMenuItem10 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenuItem11 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jMenu1.setText("File");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("New");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setText("Open");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setText("Save");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem3);
jMenu1.add(jSeparator2);
jMenuItem4.setText("Exit");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem4);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem5.setText("Undo");
jMenu2.add(jMenuItem5);
jMenu2.add(jSeparator1);
jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem6.setText("Cut");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem6);
jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem7.setText("Copy");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem7ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem7);
jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem8.setText("Paste");
jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem8ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem8);
jMenuBar1.add(jMenu2);
jMenu3.setText("Format");
jMenuItem9.setText("UpperCase");
jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem9ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem9);
jMenuItem10.setText("LowerCase");
jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem10ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem10);
jMenuBar1.add(jMenu3);
jMenu4.setText("Help");
jMenuItem11.setText("About");
jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem11ActionPerformed(evt);
}
});
jMenu4.add(jMenuItem11);
jMenuBar1.add(jMenu4);
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, 716, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("");
// TODO add your handling code here:
} 
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
FileDialog fd = new FileDialog(NotePad.this, "Select File", FileDialog.LOAD);
fd.show();
String sts = null;
if (fd.getFile() != null) {
sts = fd.getDirectory() + fd.getFile();
setTitle(sts);
BufferedReader br = null;
StringBuffer str = new StringBuffer("");
try {
br = new BufferedReader(new FileReader(sts));
String line;
try {
while ((line = br.readLine()) != null) {
str.append(line + "\n");
}
} catch (IOException ex) {
Logger.getLogger(NotePad.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(NotePad.class.getName()).log(Level.SEVERE, null, ex);
}
String t = str.toString();
jTextArea1.setText(t);
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(NotePad.class.getName()).log(Level.SEVERE, null, ex);
}
}
} 
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
FileDialog fd = new FileDialog(NotePad.this, "Save As", FileDialog.SAVE);
fd.show();
String filename = fd.getDirectory() + fd.getFile();
setTitle(filename);
String lne = jTextArea1.getText();
DataOutputStream dp = null;
try {
dp = new DataOutputStream(new FileOutputStream(filename));
} catch (FileNotFoundException ex) {
Logger.getLogger(NotePad.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader br = new BufferedReader(new StringReader(lne));
try {
while ((br.readLine()) != null) {
dp.writeBytes(lne + "\r\n");
dp.close();
}
} catch (IOException ex) {
Logger.getLogger(NotePad.class.getName()).log(Level.SEVERE, null, ex);
}
// TODO add your handling code here:
} 
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
// TODO add your handling code here:
} 
private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
cp = jTextArea1.getSelectedText();
jTextArea1.replaceRange("", jTextArea1.getSelectionStart(), jTextArea1.getSelectionEnd());
// TODO add your handling code here:
} 
private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.replaceRange(cp, jTextArea1.getSelectionStart(), jTextArea1.getSelectionEnd());
// TODO add your handling code here:
} 
private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
cp = jTextArea1.getSelectedText();
// TODO add your handling code here:
} 
private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {
String up = jTextArea1.getSelectedText();
String cvrt = up.toUpperCase();
jTextArea1.replaceSelection(cvrt);
// TODO add your handling code here:
} 
private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {
String low = jTextArea1.getSelectedText();
String cv = low.toLowerCase();
jTextArea1.replaceSelection(cv);
// TODO add your handling code here:
}
private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {
final JFrame f = new JFrame("About");
JPanel p1, p2;
JButton close = new JButton("Close");
p1 = new JPanel();
p2 = new JPanel();
f.setVisible(true);
f.setSize(300, 200);
JTextArea j1 = new JTextArea(6, 6);
String abt = "Version:1.0" + "\nDeveloper:Arunkumar.P";
j1.append(abt);
j1.setEditable(false);
p1.add(j1);
p1.setBackground(Color.pink);
//p2.add(close);
p2.setBackground(Color.red);
f.getContentPane().add(p1, BorderLayout.CENTER);
f.getContentPane().add(p2, BorderLayout.SOUTH);
close.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
// TODO add your handling code here:
}
/**
 * @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);
}
});
}
// Variables declaration - do not modify 
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem10;
private javax.swing.JMenuItem jMenuItem11;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.