QrDroid


Wednesday, 11 May 2016

Create a Notepad in Java

This tutorial is on how to create a simple Notepad application in Java.  A simple notepad that supports files openings, creations, savings and save as document. It also support Select All/Cut/Copy/Paste/Font Size/ Family/Style/Check the File Size/  operations. Another operations supported are letter count and lines count operations. Download Source Code

Notepad project is a desktop application which is implemented in Java platform.You can download Notepad desktop application project in Java with source code . This Java Project is for desktop mini and major project with source code.  This source code is imported in eclipse, netbeans for application development.

The only difference being that, this editor has been created using JAVA for the front-end interface. The text edited in the editor is stored in the desired location.

Notepad in Java features: 
  • Menu Bar for File, Edit, Format and View.
  • To create a new document.txt file.
  • Save a .txt file.
  • Edit texts with cut, copy and paste function with dialog box .
  • Format texts with Changing the font size , font family and font style.
  • View file Status With Size , Count the number of lines and Count the letter.

Step 1:
Firstly import the package of java files.
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

java.io.*; package :- The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java.

java.util.*; package :-  contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes.

java.swing.*; package :-  javax. (Java X) The prefix used for a package of Java standard extensions. For example, javax.servlet contains the classes and interfaces for running servlets, while javax.ejb is the standard extension for Enterprise JavaBeans.

java.awt.*; package :- provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

java.awt.event.*; package :- Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.

java.applet.Applet.*; package :- Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

Step 2:
First we want to create  a class :notepad .  Inside the notepad class, we will create our objects within notepad constructor. We want a TextArea which is the area containing the file information and MenuBar containing the MenuItems like :- File, Edit and etc. The JFrame will lead to our Notepad class which will contain the Notepad code.
public class notepad extends KeyAdapter  implements ActionListener, KeyListener
{
 
 static int active =0;
 static int fsize=17;
 
 JFrame frame1;
 JMenuBar npMenuBar;
 JMenu file, edit, format, view;
 JMenuItem newdoc, opendoc, exit, savedoc, saveasdoc, copydoc, pastedoc, remdoc,  fontfamily, fontstyle, fontsize, status;
 JTextArea maintext;
 JTextField title;
 Font font1;
 JPanel bottom;
 JLabel details, pastecopydoc;
 JList familylist, stylelist, sizelist;
 JScrollPane sb;

 String familyvalue[]={"Agency FB","Antiqua","Architect","Arial","Calibri","Comic Sans","Courier","Cursive","Impact","Serif"};
 String sizevalue[]={"5","10","15","20","25","30","35","40","45","50","55","60","65","70"};
 int [] stylevalue={ Font.PLAIN, Font.BOLD, Font.ITALIC };
 String [] stylevalues={ "PLAIN", "BOLD", "ITALIC" };
 String ffamily, fsizestr, fstylestr;
 int fstyle;
 int cl;
 int linecount;
 String tle ;
 String topicstitle = "";
 JScrollPane sp;
 
notepad(){

 frame1 = new JFrame("Notepad Fast");

 font1=new Font("Arial",Font.PLAIN,17);

 bottom = new JPanel();
 details = new JLabel();
 pastecopydoc = new JLabel();

 familylist = new JList(familyvalue);
 stylelist = new JList(stylevalues);
 sizelist = new JList(sizevalue);


 familylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 sizelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 stylelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

 bottom.add(details);

 maintext = new JTextArea();

 sp=new JScrollPane(maintext);
 title = new JTextField(100);

 sb = new JScrollPane(maintext);

 maintext.setMargin( new Insets(5,5,5,5) );

 maintext.setFont(font1);
 frame1.add(maintext);

 npMenuBar = new JMenuBar();

 file = new JMenu("File");
 edit = new JMenu("Edit");
 format = new JMenu("Format");
 view = new JMenu("View");

 newdoc = new JMenuItem("New Document");
 opendoc = new JMenuItem("Open Document");
 savedoc = new JMenuItem("Save Document");
 saveasdoc = new JMenuItem("Save As Document");
 exit = new JMenuItem("Exit");

 copydoc = new JMenuItem("Copy Document");
 remdoc = new JMenuItem("Remove Document");
 pastedoc = new JMenuItem("Paste Document");

 fontfamily = new JMenuItem("Set Font Family");
 fontstyle = new JMenuItem("Set Font Style");
 fontsize = new JMenuItem("Set Font Size");
 status = new JMenuItem("Status");

 file.add(newdoc);
 file.add(opendoc);
 file.add(savedoc);
 file.add(saveasdoc);
 file.add(exit);

 edit.add(copydoc);
 edit.add(pastedoc);
 edit.add(remdoc);

 format.add(fontfamily);
 format.add(fontstyle);
 format.add(fontsize);

 view.add(status);

 npMenuBar.add(file);
 npMenuBar.add(edit);
 npMenuBar.add(format);
 npMenuBar.add(view);

 frame1.setJMenuBar(npMenuBar);
 frame1.add(bottom, BorderLayout.SOUTH);

 newdoc.addActionListener(this);
 copydoc.addActionListener(this);
 pastedoc.addActionListener(this);
 remdoc.addActionListener(this);
 status.addActionListener(this);
 savedoc.addActionListener(this);
 saveasdoc.addActionListener(this);

 fontfamily.addActionListener(this);
 fontsize.addActionListener(this);
 fontstyle.addActionListener(this);

 exit.addActionListener(this);

 maintext.addKeyListener(this);

 frame1.setSize(600,600);     
 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame1.setLocationRelativeTo(null);
 frame1.setVisible(true);
}
For each option, we created a ActionListener and give the appropriate code for each.

Step 3:
Create the ActionPerformed method and then give the command to perform the action.
public void actionPerformed(ActionEvent ae)
{
 if(ae.getSource()== newdoc)
 {
 frame1.setTitle("New Document.txt");
 maintext.setText("");
 }
 else if (ae.getSource()== copydoc)
 {
  String texts= maintext.getText();
  pastecopydoc.setText(texts);
  JOptionPane.showMessageDialog(null, "Copy Text "+texts);
 }
 else if(ae.getSource()== remdoc)
 {
  maintext.setText("");
  JOptionPane.showMessageDialog(null, "Removed");
 }
 else if (ae.getSource() == pastedoc)
 {
  if(maintext.getText().length() != 0)
  {
   maintext.setText(maintext.getText());
  }
  else
  {
  maintext.setText(pastecopydoc.getText());
  }
 }
 else if(ae.getSource()== status)
 {
  try{
   if(active ==0)
   {
    File f = new File(tle+".txt");
    details.setText("Size: "+f.length());
   }
  }
  catch (Exception e)
  {
   
  }
 }
 else if (ae.getSource()== fontfamily)
  {

     JOptionPane.showConfirmDialog(null, familylist, "Choose Font Family", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  ffamily=String.valueOf(familylist.getSelectedValue());
  font1=new Font(ffamily,fstyle,fsize);
  maintext.setFont(font1);
  }
 else if (ae.getSource()== fontstyle)
  {

     JOptionPane.showConfirmDialog(null, stylelist, "Choose Font Style", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  fstyle=stylevalue[stylelist.getSelectedIndex()];
  font1=new Font(ffamily,fstyle,fsize);
  maintext.setFont(font1);
  }
 else if (ae.getSource()== fontsize)
  {

     JOptionPane.showConfirmDialog(null, sizelist, "Choose Font Size", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  fsizestr=String.valueOf(sizelist.getSelectedValue());
  fsize =Integer.parseInt(fsizestr);
  font1=new Font(ffamily,fstyle,fsize);
  maintext.setFont(font1);
  }
 else if(ae.getSource()==exit)
  {
   frame1.dispose();
  }
 
}

Step 4:
Create the keyTyped method. This method is to count the letters and lines with keyboard's key press.
public void keyTyped(KeyEvent ke){
 cl= maintext.getText().length();
 linecount = maintext.getLineCount();
 details.setText("Length: "+cl+" Line: "+linecount);
}
Step 5:
public static void main(String ar[]) 
{
  new notepad();
}
}
Project Complete!
That's it! Below is the full code .Download the files. Thanks for reading!

6 comments:


  1. Thank for sharing this tutorial also giving the source code for notepad. #sharecodepoint i was surfing and found your blog post… nice! I love your blog.

    ReplyDelete
  2. Nice interface makes good use of screen space. all features are use on this project nice and thank for this article. Really Inspired,Keep going my best wishes are with u sharecodepoint and sachin yadav

    ReplyDelete
    Replies
    1. Thank you for your feedback and interest and thank for your wishing for me.

      Delete
  3. A very light weight notepad application with extensive usage of swing components.Nice features.More features can be embedded like:"Highlighting of text","Text Background Color".Overall a good application.

    ReplyDelete
    Replies
    1. Sir ,thank you for reading my article and for your feedback. Your feedback its most important for me. sir those features (highlighting , text background color) would be added soon.

      Delete

 

Version 20.2.1 - 2016 - 2018 - © SSL Technologies And Services Private Limited | Made in India. | Copyright Content