NotePad Project in Java

Hi Friends... A Notepad Project.. Suggest me if any modification and bugs are there. Feel free to query.





package notepad;
//NOTEPAD PROJECT 
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import java.awt.*;
//FOR UNDO
import java.io.*;
import javax.swing.filechooser.FileFilter; //FOR FILE CHOOSER FOR OPEN AND SAVE MENU 
import javax.swing.filechooser.FileNameExtensionFilter; //FILTER FOR FILE CHOOSER 
import java.util.*;
public class notepad extends JFrame implements ActionListener {
public static JTextArea area;
JMenuBar mb;
JMenu file, edit, format, help;
JMenuItem new1, open, save, saveas, exit1, find, findn, delete, got, selectA, timedate, cut1, copy1, paste1, font,
vhelp, about;
JCheckBoxMenuItem wordwrap;
JScrollPane sp;
String filename = "Untitled";// DEFAULT DOCUMENT TITLE
public static UndoManager undo = new UndoManager();
public static UndoAction undoAction = new UndoAction();
boolean opened = false;
String wholetext, findstring = null;
int ind = 0;
public notepad() {
area = new JTextArea(); // TEXT AREA
mb = new JMenuBar(); // MENU BAR
// SETTING MENUBAR
file = new JMenu("File");
edit = new JMenu("Edit");
format = new JMenu("Format");
help = new JMenu("Help");
// SETTING FILE MENU
new1 = new JMenuItem("New");
open = new JMenuItem("Open");
save = new JMenuItem("Save");
saveas = new JMenuItem("Save As");
exit1 = new JMenuItem("Exit");
// SETTING EDIT MENU
cut1 = new JMenuItem("Cut");
copy1 = new JMenuItem("Copy");
paste1 = new JMenuItem("Paste");
delete = new JMenuItem("Delete");
find = new JMenuItem("Find");
findn = new JMenuItem("Find Next");
got = new JMenuItem("Go To");
selectA = new JMenuItem("Select All");
timedate = new JMenuItem("Time Date");
// SETTING FORMAT MENU
wordwrap = new JCheckBoxMenuItem("Word Wrap");
font = new JMenuItem("Font");
// SETTING HELP MENU
vhelp = new JMenuItem("View Help");
about = new JMenuItem("About");
// SETTING SHORTCUT KEY TO MENUS IN MAIN MENUBAR
//file.setMnemonic(KeyEvent.VK_Z);
//edit.setMnemonic(KeyEvent.VK_E);
//format.setMnemonic(KeyEvent.VK_D);
//help.setMnemonic(KeyEvent.VK_H);
// SETTING SHORTCUT KEY TO MENUS OF FILE MENU
new1.setMnemonic(KeyEvent.VK_N);
open.setMnemonic(KeyEvent.VK_O);
save.setMnemonic(KeyEvent.VK_S);
saveas.setMnemonic(KeyEvent.VK_Q);
exit1.setMnemonic(KeyEvent.VK_L);
// SETTING SHORTCUT KEY TO MENUS OF EDIT MENU
cut1.setMnemonic(KeyEvent.VK_X);
copy1.setMnemonic(KeyEvent.VK_C);
paste1.setMnemonic(KeyEvent.VK_P);
delete.setMnemonic(KeyEvent.VK_D);
find.setMnemonic(KeyEvent.VK_F);
findn.setMnemonic(KeyEvent.VK_B);
got.setMnemonic(KeyEvent.VK_G);
selectA.setMnemonic(KeyEvent.VK_A);
timedate.setMnemonic(KeyEvent.VK_T);
// SETTING SHORTCUT KEY TO MENUS OF FORMAT MENU
wordwrap.setMnemonic(KeyEvent.VK_W);
font.setMnemonic(KeyEvent.VK_I);
// SETTING SHORTCUT KEY TO MENUS OF HELP MENU
vhelp.setMnemonic(KeyEvent.VK_H);
about.setMnemonic(KeyEvent.VK_C);
// SETTING ACCELERATOR KEY ON SOME MENUS
new1.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
open.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
save.setAccelerator(KeyStroke.getKeyStroke('S', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
cut1.setAccelerator(KeyStroke.getKeyStroke('X', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
copy1.setAccelerator(KeyStroke.getKeyStroke('C', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
paste1.setAccelerator(KeyStroke.getKeyStroke('V', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
find.setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
selectA.setAccelerator(KeyStroke.getKeyStroke('A', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
// ENABLING CLOSE BUTTON WITH VERIFICATION
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ext();
}
});
setTitle(filename);// SETTING TITLE
sp = new JScrollPane(area);// ADDING AREA TO SCROLLPANE
setLayout(new BorderLayout()); // SETTING LAYOUT
setSize(500, 650); // SETTING DEFAULT SIZE
setLocationRelativeTo(null); // DEFAULT LOCATION TO CENTER
// ADDING MENU ITEMS TO MENUBAR
mb.add(file);
mb.add(edit);
mb.add(format);
mb.add(help);
// ADDING MENU ITEMS TO FILE MENU
file.add(new1);
file.add(open);
file.add(save);
file.add(saveas);
file.add(exit1);
// ADDING MENU ITEMS TO EDIT MENU
edit.add(undoAction);
edit.add(cut1);
edit.add(copy1);
edit.add(paste1);
edit.add(delete);
edit.add(find);
edit.add(findn);
edit.add(got);
edit.add(selectA);
edit.add(timedate);
// ADDING MENU ITEMS TO FORMAT MENU
format.add(wordwrap);
format.add(font);
// ADDING MENU ITEMS TO HELP MENU
help.add(vhelp);
help.add(about);
add("North", mb);
add("Center", sp);
// ADDING ACTIONLISTENER TO FILE MENU
new1.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
saveas.addActionListener(this);
exit1.addActionListener(this);
// ADDING ACTIONLISTENER TO EDIT MENU
cut1.addActionListener(this);
copy1.addActionListener(this);
paste1.addActionListener(this);
delete.addActionListener(this);
find.addActionListener(this);
findn.addActionListener(this);
got.addActionListener(this);
selectA.addActionListener(this);
timedate.addActionListener(this);
// ADD ACTIONLISTENERS TO FORMAT MENU
font.addActionListener(this);
wordwrap.addActionListener(this);
// ADD ACTIONLISTENERS TO HELP MENU
vhelp.addActionListener(this);
about.addActionListener(this);
// ADDING UNDO LISTENER
area.getDocument().addUndoableEditListener(new UndoListener());
// SETTING DEFAULT MARGIN TO TEXT AREA
area.setMargin(new java.awt.Insets(2, 2, 2, 2));
setVisible(true); // SETTING VISIBLE TRUE
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == new1) // NEW FILE
{
newf();
}
if (ae.getSource() == open) // OPEN A FILE
{
openn();
}
if (ae.getSource() == save) // SAVING A FILE
{
save();
}
if (ae.getSource() == saveas) // SAVE AS A FILE
{
opened = false;
save();
}
if (ae.getSource() == exit1) // EXIT OPTION
{
ext();
}
if (ae.getSource() == cut1) // CUT OPTION
{
area.cut();
}
if (ae.getSource() == copy1) // COPY OPTION
{
area.copy();
}
if (ae.getSource() == paste1) {
area.paste();
}
if (ae.getSource() == delete) {
// PASTE OPTION
// DELETE OPTION
area.replaceSelection("");
}
if (ae.getSource() == find) // FIND OPTION
{
find(0,0);
}
if (ae.getSource() == findn) // FIND NEXT OPTION
{
findn();
}
if (ae.getSource() == got) // GO TO OPTION
{
int lineNumber = 0;
try {
lineNumber = area.getLineOfOffset(area.getCaretPosition()) + 1;
String tempStr = JOptionPane.showInputDialog(this, "Enter Line Number:", "" + lineNumber);
if (tempStr == null) {
return;
}
lineNumber = Integer.parseInt(tempStr);
area.setCaretPosition(area.getLineStartOffset(lineNumber - 1));
} catch (Exception e) {
}
}
if (ae.getSource() == selectA) // SELECT ALL OPTION
{
area.selectAll();
}
if (ae.getSource() == timedate) // TIME DATE OPTION
{
area.insert(new Date().toString(), area.getSelectionStart());
}
if (ae.getSource() == wordwrap) // WORD WRAP OPTION
{
area.setLineWrap(wordwrap.isSelected());
}
if (ae.getSource() == font) // FONT OPTION
{
fontDialogBox fonts = new fontDialogBox();
}
if (ae.getSource() == vhelp) // HELP OPTION
{
JOptionPane.showMessageDialog(this, "Visit:- Artutorials.blogspot.com");
}
if (ae.getSource() == about) // ABOUT OPTION
{
JOptionPane.showMessageDialog(this, "Notepad Version 1.0");
}
}
public void newf() // FUNCTION FOR NEW FILE
{
if (!area.getText().equals("")) {
opened = false;
int confirm = JOptionPane.showConfirmDialog(null,
"Text in the " + filename + " has been changed. \n Do you want to save the changes?", "NewFile",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
save();
area.setText(null);
filename = "Untitled";
} else if (confirm == JOptionPane.NO_OPTION) {
area.setText(null);
filename = "Untitled";
}
else if (confirm == JOptionPane.CANCEL_OPTION) {
}
}
}
public void openn() {
// FUNCTION FOR OPEN FILE
JFileChooser fc = new JFileChooser();
FileFilter ft = new FileNameExtensionFilter("Text Files", "txt", "html", "php", "css", "javascript", "rtf",
"java");
fc.addChoosableFileFilter(ft);
fc.setAcceptAllFileFilterUsed(false);
int i = fc.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
String p = f.getPath();
setTitle(p);
try {
BufferedReader br = new BufferedReader(new FileReader(p));
String s1 = " ";
String s2 = " ";
while ((s1 = br.readLine()) != null) {
s2 = s2 + s1 + "\n";
}
area.setText(null);
filename = "Untitled";
area.setText(s2);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Unable to open file content.");
}
}
}
public void save() // FUNCTION FOR SAVE FILE
{
if (opened == true) {
try {
FileWriter f1 = new FileWriter(filename);
f1.write(area.getText());
f1.close();
opened = true;
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, "Requested File Not Found", "Error Dialog Box",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ioe) {
ioe.printStackTrace();
}
} else {
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
int result = fc.showSaveDialog(new JPanel());
if (result == JFileChooser.APPROVE_OPTION) {
filename = String.valueOf(fc.getSelectedFile());
setTitle(filename);
try {
FileWriter f1 = new FileWriter(filename);
f1.write(area.getText());
f1.close();
opened = true;
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, "Requested File Not Found", "Error Dialog Box",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
public void ext() // FUNCTION FOR EXIT
{
if (!area.getText().equals("")) {
int confirm = JOptionPane.showConfirmDialog(null,
"Text in the " + filename + " has been changed.\n Do you want to save the changes?", "Exit",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
save();
dispose();
System.exit(0);
} else if (confirm == JOptionPane.CANCEL_OPTION) {
String s = area.getText();
notepad n = new notepad();
n.setVisible(true);
n.area.setText(s);
} else if (confirm == JOptionPane.NO_OPTION) {
dispose();
System.exit(0);
}
} else {
System.exit(0);
}
}
public void find(int xxx,int zzz) // FUNCTION FOR FIND
{
if(xxx==10){
sel(findstring,zzz);
}
else{
findstring = JOptionPane.showInputDialog(null, "Find :", "Find", JOptionPane.INFORMATION_MESSAGE);
sel(findstring,0);
}
}
void sel(String ss, int xx){
try{
wholetext = area.getText();
ind = wholetext.indexOf(ss,xx);
if(ind==-1){
JOptionPane.showMessageDialog(this, findstring+" Not Found", "Information Dialog Box",
JOptionPane.WARNING_MESSAGE);
}else{
area.setCaretPosition(ind);
area.setSelectionStart(ind);
area.setSelectionEnd(ind + ss.length());
}
}catch(Exception e){
System.out.println(e);
}
}
public void findn() // FUNCTION FOR FIND NEXT
{
find(10,area.getCaretPosition());
}
public static void main(String s[]) // MAIN FUNCTION
{
notepad n = new notepad();
}
}
// CLASS FOR FONT OPTION
class fontDialogBox extends JFrame implements ActionListener {
// GETTING ALL THE AVAILABLE FONTS
String afs[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JList fontList = new JList(afs);
JLabel fontLabel = new JLabel("Font");
JTextField valueFont = new JTextField("Arial");
JScrollPane fontPane = new JScrollPane(fontList);
String fss[] = { "Normal", "Bold", "Italic", "Bold Italic" };
JList styleList = new JList(fss);
JLabel styleLabel = new JLabel("Style");
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane stylePane = new JScrollPane(styleList, v, h);
JTextField valueStyle = new JTextField("Normal");
String fsz[] = { "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30" };
JList sizeList = new JList(fsz);
JLabel sizeLabel = new JLabel("Font Size");
JTextField sizeFont = new JTextField("12");
JScrollPane sizePane = new JScrollPane(sizeList);
JButton okb = new JButton("OK");
JButton canb = new JButton("Cancel");
JLabel sample = new JLabel("Sample:");
JTextField samplef = new JTextField(" AaBbCc");
Font selectedfont;
public fontDialogBox() {
setSize(500, 300);
setTitle("Font");
setVisible(true);
samplef.setEditable(false);
getContentPane().setLayout(null);
fontLabel.setBounds(10, 10, 170, 20);
valueFont.setBounds(10, 35, 170, 20);
fontPane.setBounds(10, 60, 170, 150);
styleLabel.setBounds(200, 10, 100, 20);
valueStyle.setBounds(200, 35, 100, 20);
stylePane.setBounds(200, 60, 100, 150);
sizeLabel.setBounds(320, 10, 150, 20);
sizeFont.setBounds(320, 35, 50, 20);
sizePane.setBounds(320, 60, 50, 150);
okb.setBounds(400, 35, 80, 20);
canb.setBounds(400, 60, 80, 20);
sample.setBounds(150, 235, 80, 40);
samplef.setBounds(200, 235, 100, 30);
getContentPane().add(fontLabel);
getContentPane().add(fontPane);
getContentPane().add(valueFont);
getContentPane().add(styleLabel);
getContentPane().add(stylePane);
getContentPane().add(valueStyle);
getContentPane().add(sizeLabel);
getContentPane().add(sizePane);
getContentPane().add(sizeFont);
getContentPane().add(okb);
getContentPane().add(canb);
getContentPane().add(sample);
getContentPane().add(samplef);
okb.addActionListener(this);
canb.addActionListener(this);
fontList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
valueFont.setText(fontList.getSelectedValue().toString());
selectedfont = new Font(valueFont.getText(), styleList.getSelectedIndex(),
Integer.parseInt(sizeFont.getText()));
samplef.setFont(selectedfont);
}
}
});
styleList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
valueStyle.setText(styleList.getSelectedValue().toString());
selectedfont = new Font(valueFont.getText(), styleList.getSelectedIndex(),
Integer.parseInt(sizeFont.getText()));
samplef.setFont(selectedfont);
}
}
});
sizeList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
sizeFont.setText(sizeList.getSelectedValue().toString());
selectedfont = new Font(valueFont.getText(), styleList.getSelectedIndex(),
Integer.parseInt(sizeFont.getText()));
samplef.setFont(selectedfont);
}
}
});
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == okb) {
selectedfont = new Font(valueFont.getText(), styleList.getSelectedIndex(),
Integer.parseInt(sizeFont.getText()));
notepad.area.setFont(selectedfont);
setVisible(false);
}
if (ae.getSource() == canb) {
setVisible(false);
}
}
}
class UndoListener implements UndoableEditListener // CLASS FOR UNDO OPTION
{
public void undoableEditHappened(UndoableEditEvent e) {
notepad.undo.addEdit(e.getEdit());
notepad.undoAction.update();
}
}
class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
}
public void actionPerformed(ActionEvent e) {
try {
notepad.undo.undo();
} catch (CannotUndoException ex) {
System.out.println("Unable to Undo :" + ex);
ex.printStackTrace();
}
update();
}
protected void update() {
if (notepad.undo.canUndo()) {
setEnabled(true);
putValue("Undo", notepad.undo.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
Execute and Enjoy 

Comments

Popular posts from this blog

Components of C language

Decision making using if statement

How to make payment for final certificate, migration, provisional for the students of last semester of ptu?