728x90 AdSpace

Trending

Creating A Basic Notepad Application

As a school kid everyone of us would have known a computer as a machine to "PAINT" or to copy some school notes to "Notepad" .Not only kids but the influence of these tools is found everywhere.
This requirement of notepad shows the importance of it.
Every programmer shall be interested in design of such important tool, so are we! 
The basic design pattern of notepad is divided as Menu bar and the text filed .
The picture given below describes the method of design .  



Before getting into the design details we need to know about Menu bar .
Swing provides support for pull-down and popup menus. A JMenubar can contain several JMenu ‘s. Each of the JMenu ‘s can contain a series of JMenuItem ‘s that you can select.
Here goes the code

/*source:ba-programmer.blogspot.com
hope you like it ...:)
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 class Notepad{
JFrame frame;
JTextArea area;
JMenuBar menubar;
JMenu menus[];
JMenuItem item;
Notepad(){
frame=new JFrame("Notepad");
menubar=new JMenuBar();
menus=new JMenu[5];//file edit format view help
area=new JTextArea("",100,500);
go();
}
public void go(){
menus[0]=new JMenu("File");
menus[1]=new JMenu("Edit");
menus[2]=new JMenu("Format");
menus[3]=new JMenu("View");
menus[4]=new JMenu("Help");
JScrollPane pane=new JScrollPane(area);
//adding items to file menu
item=new JMenuItem("new");
item.addActionListener(new Operations("new"));
menus[0].add(item);
item=new JMenuItem("open");
item.addActionListener(new Open());
menus[0].add(item);
item=new JMenuItem("save");
item.addActionListener(new Save());
menus[0].add(item);
item=new JMenuItem("exit");
item.addActionListener(new Operations("exit"));
menus[0].add(item);
//adding items to format menu
item=new JMenuItem("Word Wrap");
item.addActionListener(new Operations("wordwrap"));
menus[2].add(item);
item=new JMenuItem("bold");
item.addActionListener(new Operations("font"));
menus[2].add(item);
item=new JMenuItem("italic");
item.addActionListener(new Operations("italic"));
menus[2].add(item);

//adding items to help menu
item=new JMenuItem("ba-programmer.blogspot.in");
menus[4].add(item);

for(int  i=0;i<5 i="">
menubar.add(menus[i]);
frame.setJMenuBar(menubar);
frame.add(pane);
frame.setSize(1350,730);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class Open implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser file=new JFileChooser();
file.showOpenDialog(frame);
File f=file.getSelectedFile();
String line;
try{
BufferedReader reader=new BufferedReader(new FileReader(f));
area.setText(reader.readLine());
while((line=reader.readLine())!=null)
area.setText(area.getText()+"\n"+line);
}catch(Exception eee){
JOptionPane.showMessageDialog(null,"Error");
}

}
}
class Save implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser file=new JFileChooser();
file.showSaveDialog(frame);
File s=file.getSelectedFile();
try{
BufferedWriter writer=new BufferedWriter(new FileWriter(s));
writer.write(area.getText()+" \n ");
writer.close();
}catch(Exception e1){
JOptionPane.showMessageDialog(frame,"Error");
}
}
}
class Operations implements ActionListener
{
int i=13;
Font f;
String oper;
Operations(String oper){
this.oper=oper;
}
public void actionPerformed(ActionEvent e)
{
if(oper=="new")
area.setText("");
else if(oper=="exit")
System.exit(0);
else if(oper=="font")
{
i=Integer.parseInt(JOptionPane.showInputDialog(frame,"Enterfontsize"));
f=new Font("Time New Roman",Font.BOLD,i);area.setFont(f);
}
else if(oper=="italic")
f=new Font("Time New Roman",Font.ITALIC,i);area.setFont(f);

}
}
}
public class NotepadDemo{
public static void main(String arg[]){
Notepad note=new Notepad();
}
}
Click Here to download class Files.
Stay Connected.
Follow us Google+.

Creating A Basic Notepad Application Reviewed by B AProgrammer on 11:11 Rating: 5 As a school kid everyone of us would have known a computer as a machine to "PAINT" or ...

1 comment:

  1. Why cant you provide a tutorials on GUI components ?

    ReplyDelete