Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Noteopad in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


bhagirath singh
Noteopad in java
2 Answer(s)      4 years ago
Posted in : Java Beginners


source code of notepad in java.


Advertisement
View Answers

December 17, 2010 at 12:12 PM


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;
}


December 17, 2010 at 12:12 PM


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



Related Tutorials/Questions & Answers:
Noteopad in java
Noteopad in java  source code of notepad in java.   Hello... ().toLowerCase ().endsWith (".java")|| file.isDirectory (); } public String getDescription () { return "Only Java Files (*.java)"; } } public class Notepad
java
java  diff bt core java and java
Advertisements
java
java  what is java
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
 

 

 

DMCA.com