Blog

Notepad in Java

Posted by rahul jaiswal Blogs | Java Aug 26, 2012
This is a sample code for Notepad in Java.
Download Files: notepad.zip



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
class popup extends JFrame implements ActionListener
{
final JDesktopPane desk;
JButton b,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11;
JPopupMenu popmenu;
JToolBar tb;
JScrollPane scroll;
JTextArea txt;
JMenuItem cut,copy,paste;
JMenu file,edit,format,view,help;
JMenuItem  new1,open,save,saveas,print,pageset,exit;
JMenuItem cut1,copy1,paste1;
JMenuItem font;
JMenuItem viewhelp,abtnotepad;
JMenuBar menubar;
JCheckBoxMenuItem bold,italic,underline,word;
JComboBox cb;
String a[] ={"1","2","3"};
    popup()
{
 setTitle("Notepad++");
 popmenu=new JPopupMenu();
 txt=new JTextArea();
 scroll = new JScrollPane(txt);
 txt.setLineWrap(true);
 //popupmenu
 cut=new JMenuItem("Cut", new ImageIcon("cut.jpg"));
 copy=new JMenuItem("Copy", new ImageIcon("copy.jpg"));
 paste=new JMenuItem("Paste",new ImageIcon("paste.jpg"));
 //menubar
 menubar=new JMenuBar();
 ImageIcon icon = new ImageIcon("notepad.jpg");
 setIconImage(icon.getImage());
      file = new JMenu ("File");
 file.setMnemonic(KeyEvent.VK_F);
      edit=new JMenu("Edit");
 edit.setMnemonic(KeyEvent.VK_E);
 format=new JMenu("Format");
 format.setMnemonic(KeyEvent.VK_T);
 view=new JMenu("View");
 view.setMnemonic(KeyEvent.VK_V);
 help=new JMenu("Help");
      help.setMnemonic(KeyEvent.VK_H);
 //addmenu
      menubar.add(file);
 menubar.add(edit);
 menubar.add(format);
 menubar.add(view);
 menubar.add(help);
 setJMenuBar(menubar);

 //menuitem file
 new1=new JMenuItem("New", new ImageIcon("new.jpg"));
 new1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));
 new1.addActionListener(this);
 //open
 open=new JMenuItem("Open" , new ImageIcon("open.jpg"));
 open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));
 //open.addActionListener(this);
//save
 save=new JMenuItem("Save" ,new ImageIcon("save.jpg"));
 save.addActionListener(this);
 save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));
 //saveas
 saveas=new JMenuItem("Save as",new ImageIcon("saveas.jpg"));
 saveas.addActionListener(this);
 //pageset
 pageset=new JMenuItem("Page Setup");
 pageset.addActionListener(this);
 //print
      print=new JMenuItem("Print",new ImageIcon("print.jpg"));
 print.addActionListener(this);
 //exit
 exit=new JMenuItem("Exit");
 exit.addActionListener(this);
 exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,Event.CTRL_MASK));
 file.add(new1);
 file.add(open);
 file.add(save);
 file.add(saveas);
 file.addSeparator();
 file.add(pageset);
 file.add(print);
 file.addSeparator();
 file.add(exit);
      //edit
 cut1=new JMenuItem("Cut", new ImageIcon ("cut.jpg") );
 copy1=new JMenuItem("Copy",new ImageIcon("copy.jpg"));
 paste1=new JMenuItem("Paste" , new ImageIcon("paste.jpg"));
 edit.add(cut1);
 edit.add(copy1);
      edit.add(paste1);
 cut1.addActionListener(this);
 copy1.addActionListener(this);
 paste1.addActionListener(this);
 //format
  word=new JCheckBoxMenuItem("Word Wrap");
      format.add(word);
 //help
 viewhelp =new JMenuItem("View Help",new ImageIcon("help.jpg"));
 abtnotepad=new JMenuItem("About Notepad");
 help.add(viewhelp);
 help.add(abtnotepad);
 abtnotepad.addActionListener(this);
 //chekbox
 bold=new JCheckBoxMenuItem("Bold" ,new ImageIcon("bold.jpg"));
 italic=new JCheckBoxMenuItem("Italic" ,new ImageIcon("italic.jpg"));
 underline=new JCheckBoxMenuItem("UnderLine" ,new ImageIcon("underline.jpg"));
 JMenu nn = new JMenu("Bold");
 nn.add(bold);
 nn.add(italic);
 nn.add(underline);
 format.add(nn);
 ButtonGroup bg =new ButtonGroup();
 JRadioButtonMenuItem left=new JRadioButtonMenuItem("Left" ,new ImageIcon("left.jpg"));
      JRadioButtonMenuItem center=new JRadioButtonMenuItem("Center" ,new ImageIcon("center.jpg"));
 JRadioButtonMenuItem right=new JRadioButtonMenuItem("Right" ,new ImageIcon("right.jpg"));
  JRadioButtonMenuItem  just=new JRadioButtonMenuItem("Justify",new ImageIcon("just.jpg"));
 bg.add(left);
 bg.add(center);
 bg.add(right);
 bg.add(just);
 JMenu xxx = new JMenu("Align");
 xxx.add(left);
 xxx.add(center);
 xxx.add(right);
 xxx.add(just);
// format.add(bold);
 //format.add(italic);
 //format.add(underline);
 format.addSeparator();
 format.add(xxx);
 
 //toolbarnew 
 tb=new JToolBar(SwingConstants.VERTICAL);
 tb.setBackground(Color.green);
 tb.setLayout(new GridLayout(1,0));
 b = new JButton (new ImageIcon("new.jpg"));
 b1 = new JButton(new ImageIcon("open.jpg"));
 b2=new JButton(new ImageIcon("save.jpg"));
 b3=new JButton(new ImageIcon("cut.jpg"));
 b4=new JButton(new ImageIcon("copy.jpg"));
 b5=new JButton(new ImageIcon("paste.jpg"));
 b6=new JButton(new ImageIcon("left.jpg"));
 b6.addActionListener(this);
 b7=new JButton(new ImageIcon("center.jpg"));
 b7.addActionListener(this);
 b8=new JButton(new ImageIcon("right.jpg"));
 b8.addActionListener(this);
 b9=new JButton(new ImageIcon("help.jpg"));
 b10=new JButton(new ImageIcon("print.jpg"));
 b11=new JButton(new ImageIcon("right.jpg"));
 cb=new JComboBox(a);
 b.addActionListener(this);
 b.setActionCommand("New");
 b1.addActionListener(this);
 b2.addActionListener(this);
 b3.addActionListener(this);
 b4.addActionListener(this);
 b5.addActionListener(this);
 b6.addActionListener(this);
 b7.addActionListener(this);
 b8.addActionListener(this);
 b9.addActionListener(this);
 b10.addActionListener(this);
      b11.addActionListener(this);
 //format register.....
 italic.addActionListener(this);
 //end
 
 tb.add(b);
 tb.add(b1);
 tb.add(b2);
 tb.addSeparator();
 tb.add(cb);
 tb.add(b3);
 tb.add(b4);
 tb.add(b5);
 tb.addSeparator();
 tb.add(b6);
 tb.add(b7);
 tb.add(b8);
 tb.addSeparator();
      tb.add(b9);
 tb.add(b10);
 tb.add(b11);
 add(BorderLayout.NORTH,tb);
 desk=new JDesktopPane();
 add(desk);
 popmenu.add(cut);
 popmenu.add(copy);
 popmenu.add(paste);
 txt.addMouseListener(new MouseAdapter()
 {
      public void mouseReleased(MouseEvent e)
   {
     if (e.isPopupTrigger())
     {
  popmenu.setLocation(e.getX(),e.getY());
  popmenu.setVisible(true);
     }
   }
     });
 add(scroll);
 txt.setFont(new Font("Times New Roman",Font.BOLD,20));
      setSize(500,400);
 //setBounds(400,50,700,500);
 setVisible(true);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==italic)
{
txt.setFont(new Font ("Arial",Font.ITALIC,22));
}
if (e.getSource()==cut1 || e.getSource()==b3)
{
txt.cut();
}
if (e.getSource()==copy1 || e.getSource()==b4)
{
txt.copy();
}
if (e.getSource()==paste1 || e.getSource()==b5)
{
txt.paste();
}

if (e.getSource()==new1 ||  e.getSource()==b)
{
new popup();
}
if (e.getSource()==exit)
{
System.exit(0);
}
if (e.getSource()==abtnotepad)
{
JOptionPane op = new JOptionPane();
JOptionPane.showMessageDialog(this,"Notepad is easily to note anything your reqiurnemet","ABOUTNOTEPAD",JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource()==save)
{
try
{
FileDialog save=new FileDialog(this,"save dear",FileDialog.SAVE);
save.show();
FileWriter write = new FileWriter(save.getDirectory()+save.getFile());
BufferedWriter wr = new BufferedWriter(write);
wr.write(this.txt.getText());
wr.close();
}
catch (Exception ex)
{
System.out.println(" "+ex);
}
   
}
if (e.getSource()==open || e.getSource()==b1)
{
try
{
FileDialog fd = new FileDialog(this,"Open File",FileDialog.LOAD);
fd.show();
System.out.println(fd.getDirectory()+fd.getFile());
FileReader fr = new FileReader(fd.getDirectory()+fd.getFile());
BufferedReader br = new BufferedReader(fr);
String hi=br.readLine();
while (hi!=null)
{
txt.append(hi+"\n");
hi=br.readLine();
}
fr.close();
}
catch (Exception ex)
{
System.out.print(" "+ex);
}
}
}
public static void main(String[] args) 
{
new popup();
}
}

post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
More Blogs from this Blogger
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter