source code of notepad in java.
Hello Friend,
Try the following code:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.datatransfer.*;
import javax.swing.filechooser.FileFilter;
class FileFilterClass extends FileFilter {
public boolean accept (File file) {
return file.getName ().toLowerCase
().endsWith (".java")|| file.isDirectory ();
}
public String getDescription () {
return "Only Java Files (*.java)";
}
}
public class Notepad extends JFrame
implements ActionListener {
JMenuBar menuBar;
JMenu menu;
JMenuItem open,copy,cut,paste,save,quit;
JTextArea field;
FileFilterClass javaFileFilter = new FileFilterClass ();
File file = new File ("Hello.java");
public Notepad(String title){
super(title);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex) {
System.err.println("Error loading System specific Look and Feel. " + ex);
}
Container contentpane = getContentPane ();
contentpane.setLayout ( new BorderLayout () );
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
field = new JTextArea();
field.setDragEnabled(true);
contentpane.add ( field, "Center");
menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
open = new JMenuItem("Open");
copy = new JMenuItem("Copy");
cut = new JMenuItem("Cut");
paste = new JMenuItem("Paste");
save = new JMenuItem("Save");
quit = new JMenuItem("Quit");
menu.add(open);
menu.add(copy);
menu.add(cut);
menu.add(paste);
menu.add(save);
menu.add(quit);
menuBar.add(menu);
open.addActionListener(this);
copy.addActionListener(this);
cut.addActionListener(this);
paste.addActionListener(this);
save.addActionListener(this);
quit.addActionListener(this);
}
public static void main(String[] args){
new Notepad("Notepad").setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String cmd =(String)ae.getActionCommand();
if (cmd.equals("Open")) open();
else if (cmd.equals("Copy")) copy();
else if (cmd.equals("Cut")) cut();
else if (cmd.equals("Paste")) paste();
else if (cmd.equals("Save")) save();
else if (cmd.equals("Quit")) quit();
}
public void open() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(javaFileFilter);
int returnVal = fileChooser.showOpenDialog(this);
if(returnVal ==JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile();
String fileContent = readFile(file);
field.setText(fileContent);
}
}
public String readFile (File file) {
StringBuffer strBuffer;
String fileContent=null;
String lineString;
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
strBuffer = new StringBuffer() ;
while ((lineString = br.readLine ()) != null) {
strBuffer.append (lineString + "\n");
}
fr.close();
fileContent = strBuffer.toString();
String name = file.getName();
if(name != null) {
int extensionIndex = name.lastIndexOf('.');
setTitle(name.substring(0,extensionIndex));
}
}
catch (IOException e ) {
return null;
}
return fileContent;
}
continue..
public void copy() {
String s = field.getSelectedText();
int start=field.getSelectionStart();
int end=field.getSelectionEnd();
StringSelection ss = new StringSelection(s);
this.getToolkit().getSystemClipboard().
setContents(ss, ss);
}
public void cut() {
String s = field.getSelectedText();
int start=field.getSelectionStart();
int end=field.getSelectionEnd();
field.replaceRange("",start,end);
StringSelection ss = new StringSelection(s);
this.getToolkit().getSystemClipboard().setContents(ss,ss);
}
public void paste() {
Clipboard cb = this.getToolkit().getSystemClipboard();
Transferable tr = cb.getContents(this);
try {
String s = (String)tr.getTransferData(DataFlavor.stringFlavor);
int start=field.getSelectionStart();
int end=field.getSelectionEnd();
field.replaceRange(s,start,end);
}
catch (Exception e) {
return;
}
}
public boolean save() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter (javaFileFilter);
int returnVal =fileChooser.showSaveDialog(this);
if(returnVal ==JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile ();
if (file.exists()) {
int response =JOptionPane.showConfirmDialog (null,"File already exists. Do you want to continue?","Overwrite Confirmation",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
if (response ==JOptionPane.CANCEL_OPTION) return false;
}
String fileName = file.getName();
if(fileName != null) {
int extensionIndex =
fileName.lastIndexOf('.');
setTitle(fileName.substring(0,extensionIndex));
}
return writeFile (file, field.getText());
}
return false;
}
public static boolean writeFile (File file, String
content) {
try {
PrintWriter pw = new PrintWriter (new
BufferedWriter (new FileWriter (file)));
pw.print (content);
pw.flush ();
pw.close ();
}
catch (IOException e) {
return false;
}
return true;
}
public void quit() {
System.exit(0);
}
}
Thanks