Here is the code for "Notepad in Java Swing"
This is Notepad.java. This file contains look and feel, addition of menu/buttons and action perform of the buttons and menu.
1.
/* ****************
* Created By... *
* Santanu Kar *
* ****************/
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class Notepad extends JFrame implements ActionListener
{
Container c;
public JScrollPane sc;
public JTextArea t;
private JMenuBar menubar;
private JMenu file;
private JMenuItem file_new;
private JMenuItem file_open;
private JSeparator file_sep1;
private JMenuItem file_save;
private JMenuItem file_save_as;
private JSeparator file_sep2;
private JMenuItem file_print;
private JSeparator file_sep3;
private JMenuItem file_close;
private JMenuItem file_exit;
private JMenu edit;
private JMenuItem edit_undo;
private JMenuItem edit_redo;
private JSeparator edit_sep1;
private JMenuItem edit_copy;
private JMenuItem edit_cut;
private JMenuItem edit_paste;
private JMenuItem edit_delete;
private JSeparator edit_sep2;
private JMenuItem edit_find;
private JMenuItem edit_find_next;
private JMenuItem edit_replace;
private JSeparator edit_sep3;
private JMenuItem edit_selectall;
private JMenuItem edit_timedate;
private JMenu format;
private JMenuItem format_font;
private JMenu convert;
private JMenuItem str2uppr, str2lwr;
private JCheckBoxMenuItem format_wordwarp;
private JMenu help;
private JMenuItem help_about;
UndoManager undo = new UndoManager();
UIManager.LookAndFeelInfo lnf[];
Find finder;
FontChooser fc;
About abt;
String path, content;
public Notepad(){
super("Untitled - Notepad");
try
{
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
}
catch(Exception ex)
{
ex.printStackTrace();
}
Container c = getContentPane();
t = new JTextArea("", 5,5);
t.setFont(new Font("Verdana",Font.PLAIN, 12));
sc = new JScrollPane(t, sc.VERTICAL_SCROLLBAR_AS_NEEDED, sc.HORIZONTAL_SCROLLBAR_AS_NEEDED); //adding scrollbar to text area;
c.add(sc);
c.setBackground(Color.white);
menubar = new JMenuBar();
file = new JMenu("File");
file_new = new JMenuItem("New");
file_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
file_new.addActionListener(this);
file.add(file_new);
file_open = new JMenuItem("Open");
file_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
file_open.addActionListener(this);
file.add(file_open);
file_sep1 = new JSeparator();
file.add(file_sep1);
file_save = new JMenuItem("Save");
file_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
file_save.addActionListener(this);
file.add(file_save);
file_save_as = new JMenuItem("Save As");
file_save_as.addActionListener(this);
file.add(file_save_as);
file_sep2 = new JSeparator();
file.add(file_sep2);
file_print = new JMenuItem("Print");
file_print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
file_print.addActionListener(this);
file.add(file_print);
file_sep3 = new JSeparator();
file.add(file_sep3);
file_close = new JMenuItem("Close");
file_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.CTRL_MASK));
file_close.addActionListener(this);
file.add(file_close);
file_exit = new JMenuItem("Exit");
file_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
file_exit.addActionListener(this);
file.add(file_exit);
menubar.add(file);
edit = new JMenu("Edit");
edit_undo = new JMenuItem("Undo");
edit_undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK));
edit_undo.addActionListener(this);
edit.add(edit_undo);
edit_redo = new JMenuItem("Redo");
edit_redo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK));
edit_redo.addActionListener(this);
edit.add(edit_redo);
edit_sep1 = new JSeparator();
edit.add(edit_sep1);
edit_copy = new JMenuItem("Copy");
edit_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
edit_copy.addActionListener(this);
edit.add(edit_copy);
edit_cut = new JMenuItem("Cut");
edit_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
edit_cut.addActionListener(this);
edit.add(edit_cut);
edit_paste = new JMenuItem("Paste");
edit_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
edit_paste.addActionListener(this);
edit.add(edit_paste);
edit_delete = new JMenuItem("Delete");
edit_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
edit_delete.addActionListener(this);
edit.add(edit_delete);
edit_sep2 = new JSeparator();
edit.add(edit_sep2);
edit_find = new JMenuItem("Find");
edit_find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
edit_find.addActionListener(this);
edit.add(edit_find);
edit_find_next = new JMenuItem("Find Next");
edit_find_next.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
edit_find_next.addActionListener(this);
edit.add(edit_find_next);
edit_replace = new JMenuItem("Replace");
edit_replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
edit_replace.addActionListener(this);
edit.add(edit_replace);
edit_sep3 = new JSeparator();
edit.add(edit_sep3);
edit_selectall = new JMenuItem("Select All");
edit_selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
edit_selectall.addActionListener(this);
edit.add(edit_selectall);
edit_timedate = new JMenuItem("Time");
edit_timedate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
edit_timedate.addActionListener(this);
edit.add(edit_timedate);
menubar.add(edit);
format = new JMenu("Format");
format_font = new JMenuItem("Font");
format_font.addActionListener(this);
format.add(format_font);
convert = new JMenu("Convert");
str2uppr = new JMenuItem("To Uppercase...");
str2uppr.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK));
str2uppr.addActionListener(this);
convert.add(str2uppr);
str2lwr = new JMenuItem("To Lowercase...");
str2lwr.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK));
str2lwr.addActionListener(this);
convert.add(str2lwr);
format.add(convert);
format_wordwarp = new JCheckBoxMenuItem("Word Warp");
format_wordwarp.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK));
format_wordwarp.addActionListener(this);
format.add(format_wordwarp);
menubar.add(format);
help = new JMenu("Help");
help_about = new JMenuItem("About");
help_about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
help_about.addActionListener(this);
help.add(help_about);
menubar.add(help);
c.add(menubar, BorderLayout.NORTH);
Document doc= t.getDocument();
doc.addUndoableEditListener(
new UndoableEditListener( )
{
public void undoableEditHappened( UndoableEditEvent event )
{
undo.addEdit(event.getEdit());
}
}
);
finder = new Find(this);
finder.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
fc = new FontChooser(this);
abt = new About(this);
int w = 600;
int h = 450;
setSize(600, 450);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setLocation(center.x-w/2, center.y-h/2);
this.setVisible(true);
path = "";
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent paramAnonymousWindowEvent)
{
exitOnClose();
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==file_new)
file_new();
else if(e.getSource()==file_open)
file_open();
else if(e.getSource()==file_save)
file_save();
else if(e.getSource()==file_save_as)
file_save_as();
else if(e.getSource()==file_print)
file_print();
else if(e.getSource()==file_close)
file_close();
else if(e.getSource()==file_exit)
file_exit();
else if(e.getSource()==edit_undo)
edit_undo();
else if(e.getSource()==edit_redo)
edit_redo();
else if(e.getSource()==edit_cut)
edit_cut();
else if(e.getSource()==edit_copy)
edit_copy();
else if(e.getSource()==edit_paste)
edit_paste();
else if(e.getSource()==edit_delete)
edit_delete();
else if(e.getSource()==edit_find)
edit_find();
else if(e.getSource()==edit_find_next)
edit_find_next();
else if(e.getSource()==edit_replace)
edit_replace();
else if(e.getSource()==edit_selectall)
edit_selectall();
else if(e.getSource()==edit_timedate)
edit_timedate();
else if(e.getSource()==format_font)
format_font();
else if(e.getSource()==str2uppr)
str2uppr();
else if(e.getSource()==str2lwr)
str2lwr();
else if(e.getSource()==format_wordwarp)
format_wordwarp();
else if(e.getSource()==help_about)
help_about();
}
int value;
int option;
String name = null;
public void exitOnClose()
{
if ((!t.getText().equals("")) && (!t.getText().equals(this.content)))
{
if (name == null)
{
option = JOptionPane.showConfirmDialog(null, "Do you want to save the file?");
if (option == 0)
{
file_save_as();
System.exit(0);
}
if (option == 1)
{
System.exit(0);
}
}
else
{
option = JOptionPane.showConfirmDialog(null, "Do you want to save the file?");
if (option == 0)
{
file_save();
System.exit(0);
}
if (option == 1)
{
System.exit(0);
}
}
}
else
{
System.exit(0);
}
}
public void file_new()
{
if ((!t.getText().equals("")) && (!t.getText().equals(this.content)))
{
if (name == null)
{
option = JOptionPane.showConfirmDialog(null, "Do you want to save the file?");
if (option == 0)
{
file_save_as();
t.setText("");
}
if (option == 1)
{
t.setText("");
}
}
else
{
this.option = JOptionPane.showConfirmDialog(null, "Do you want to save the file?");
if (this.option == 0)
{
file_save();
t.setText("");
}
if (option == 1)
{
t.setText("");
}
}
}
else
{
t.setText("");
}
setTitle("Untitled - Notepad");
}
public void file_open(){
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r=fc.showOpenDialog(this);
if(r==fc.CANCEL_OPTION)
return;
File myfile = fc.getSelectedFile();
if(myfile == null || myfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this, "Select a file!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try
{
BufferedReader input = new BufferedReader(new FileReader(myfile));
StringBuffer str = new StringBuffer();
String line;
while((line = input.readLine()) != null)
str.append(line+"\n");
t.setText(str.toString());
content = t.getText();
path = myfile.toString();
setTitle(myfile.getName()+" - Notepad");
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "File not found: "+e);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, "IO ERROR: "+e);
}
}
public void file_save(){
if(path.equals(""))
{
file_save_as();
return;
}
try
{
FileWriter fw = new FileWriter(path);
fw.write(t.getText());
content = t.getText();
fw.close();
}
catch(IOException i)
{
JOptionPane.showMessageDialog(this,"Failed to save the file","Error",JOptionPane.ERROR_MESSAGE);
}
}
public void file_save_as(){
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = fc.showSaveDialog(this);
if(r==fc.CANCEL_OPTION)
return;
File myfile = fc.getSelectedFile();
if(myfile==null || myfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Please enter a file name!","Error",JOptionPane.ERROR_MESSAGE);
return;
}
if(myfile.exists())
{
r = JOptionPane.showConfirmDialog(this, "A file with same name already exists!\nAre you sure want to overwrite?");
if(r != 0)
return;
}
try
{
FileWriter fw = new FileWriter(myfile);
fw.write(t.getText());
content = t.getText();
setTitle(myfile.getName()+" - Notepad");
fw.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"Failed to save the file","Error",JOptionPane.ERROR_MESSAGE);
}
}
public void file_print() {
PrinterJob printer = PrinterJob.getPrinterJob();
//printer.setPrintable( this);
HashPrintRequestAttributeSet printAttr = new HashPrintRequestAttributeSet();
if(printer.printDialog(printAttr))
{
try
{
printer.print(printAttr);
}
catch(PrinterException e)
{
JOptionPane.showMessageDialog(this,"Failed to print the file: "+e,"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
public void file_close(){
if(t.getText().equals("") || t.getText().equals(content))
{
t.setText("");
path = "";
setTitle("Untitled - Notepad");
}
else
{
int a = JOptionPane.showConfirmDialog(null, "The text has been changed\nDo you want to save the changes?");
if(a==0)
file_save();
else if(a==1)
{
t.setText("");
path = "";
setTitle("Untitled - Notepad");
}
else if(a==2)
return;
}
}
public void file_exit(){
if(t.getText().equals("") || t.getText().equals(content))
System.exit(0);
else
{
int b = JOptionPane.showConfirmDialog(null, "The text has been changed.\nDo you want to save the changes?");
if(b==0)
file_save();
else if(b==1)
System.exit(0);
else if(b==2)
return;
}
}
public void edit_undo() {
if( undo.canUndo())
{
try
{
undo.undo();
}
catch(CannotUndoException e)
{
}
}
}
public void edit_redo(){
if( undo.canRedo())
{
try
{
undo.redo();
}
catch(CannotRedoException e)
{
}
}
}
public void edit_cut(){
t.cut();
}
public void edit_copy(){
t.copy();
}
public void edit_paste(){
t.paste();
}
public void edit_delete(){
String temp = t.getText();
t.setText(temp.substring(0, t.getSelectionStart())+temp.substring(t.getSelectionEnd()));
}
public void edit_find(){
finder.setVisible(true);
}
public void edit_find_next(){
finder.find_next();
}
public void edit_replace(){
finder.setVisible(true);
}
public void edit_selectall(){
t.selectAll();
}
public void edit_timedate(){
try
{
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("h:m:s a");
String now = sdf.format(cal.getTime());
String temp1 = t.getText().substring(0,start);
String temp2 = t.getText().substring(end);
t.setText(temp1+" "+now+" "+temp2);
t.select(start+1, start+1+now.length());
}
catch(NullPointerException e){}
}
public void format_font(){
fc.window.setVisible(true);
}
public void str2uppr(){
try
{
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
String temp1 = t.getText().substring(0,start);
String temp2 = t.getText().substring(end);
String conv = t.getSelectedText().toUpperCase();
t.setText(temp1+conv+temp2);
t.select(start, end);
}
catch(NullPointerException e){}
}
public void str2lwr(){
try
{
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
String temp1 = t.getText().substring(0,start);
String temp2 = t.getText().substring(end);
String conv = t.getSelectedText().toLowerCase();
t.setText(temp1+conv+temp2);
t.select(start, end);
}
catch(NullPointerException e){}
}
public void format_wordwarp(){
if(t.getLineWrap()==false)
t.setLineWrap(true);
else
t.setLineWrap(false);
}
public void help_about(){
abt.window.setVisible(true);
}
}
2.
FontChooser.java. This file helps you to change font, size and type of the font.
/* ***************
* Created By...
* Santanu Kar
* ****************/
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FontChooser implements ActionListener, ListSelectionListener{
static JFrame window = new JFrame("Font Chooser");
Notepad samp;
JLabel flist_label, fsize_label, fstyle_label, fprev_label, preview;
JList flist, fsize, fstyle;
ScrollPane flist_sc, fstyle_sc, fsize_sc;
JButton ok, cancel;
GraphicsEnvironment ge;
String font_names[];
Font sample;
String font_name;
int font_size, font_style;
public FontChooser(Notepad ref)
{
samp = ref;
window.setLayout(null);
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
font_names = ge.getAvailableFontFamilyNames();
flist = new JList(font_names);
flist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
flist_label = new JLabel("Font: ");
window.add(flist_label);
flist_label.setBounds(10, 10, 120, 20);
flist_sc = new ScrollPane();
flist_sc.add(flist);
flist_sc.setBounds(10, 30, 120, 200);
window.add(flist_sc);
flist.addListSelectionListener(this);
String styles[] = {"Regular", "Bold", "Italic", "Bold Italic"};
fstyle = new JList(styles);
fstyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fstyle_label = new JLabel("Style: ");
window.add(fstyle_label);
fstyle_label.setBounds(140, 10, 80, 20);
fstyle_sc = new ScrollPane();
fstyle_sc.add(fstyle);
fstyle_sc.setBounds(140, 30, 80, 70);
window.add(fstyle_sc);
fstyle.addListSelectionListener(this);
Vector<String> a = new Vector<String>(40, 1);
for (int i = 8; i <= 100; i += 2)
a.addElement(String.valueOf(i));
fsize = new JList(a);
fsize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fsize_label = new JLabel("Size: ");
fsize_label.setBounds(140, 110, 80, 20);
window.add(fsize_label);
fsize_sc = new ScrollPane();
fsize_sc.add(fsize);
fsize_sc.setBounds(140, 130, 80, 100);
window.add(fsize_sc);
fsize.addListSelectionListener(this);
ok = new JButton("OK");
ok.setBounds(230, 30, 75, 20);
ok.addActionListener(this);
window.add(ok);
cancel = new JButton("Cancel");
cancel.setBounds(230, 50, 75, 20);
cancel.addActionListener(this);
window.add(cancel);
fprev_label = new JLabel("Preview: ");
fprev_label.setBounds(10, 230, 300, 20);
window.add(fprev_label);
preview = new JLabel("Sample Text");
preview.setBounds(10, 250, 290, 85);
preview.setHorizontalAlignment(SwingConstants.CENTER);
preview.setOpaque(true);
preview.setBackground(Color.white);
preview.setBorder(new LineBorder(Color.black, 1));
window.add(preview);
window.setResizable(false);
int w = 320;
int h = 380;
window.setSize(w,h);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
window.setLocation(center.x-w/2, center.y-h/2+25);
window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
window.setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
ok();
else if(e.getSource()==cancel)
cancel();
}
public void valueChanged(ListSelectionEvent l)
{
if(l.getSource()==flist)
{
preview.setText( flist.getSelectedValue().toString() );
changeFontSample();
}
else if(l.getSource()==fsize)
{
changeFontSample();
}
else if(l.getSource()==fstyle)
{
changeFontSample();
}
}
private void changeFontSample()
{
try
{
font_name = flist.getSelectedValue().toString();
}
catch(NullPointerException npe)
{
font_name = "Verdana";
}
try
{
font_style = getStyle();
}
catch(NullPointerException npe)
{
font_style = Font.PLAIN;
}
try
{
font_size = Integer.parseInt(fsize.getSelectedValue().toString());
}
catch(NullPointerException npe)
{
font_size = 12;
}
sample = new Font(font_name, font_style, font_size);
preview.setFont(sample);
}
private int getStyle()
{
if( fstyle.getSelectedValue().toString().equals("Bold") )
return Font.BOLD;
if(fstyle.getSelectedValue().toString().equals("Italic") )
return Font.ITALIC;
if(fstyle.getSelectedValue().toString().equals("Bold Italic"))
return Font.BOLD+Font.ITALIC;
return Font.PLAIN;
}
private void ok()
{
try
{
samp.t.setFont(sample);
}
catch(NullPointerException npe){}
this.window.setVisible(false);
}
private void cancel()
{
this.window.setVisible(false);
}
}
3. Find.java. This file helps you to find, replace the texts.
/* ***************
* Created By...
* Santanu Kar
* ****************/
import java.awt.Checkbox;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Find extends JFrame implements ActionListener
{
int startIndex=0;
Label l1, l2;
TextField tf, tr;
JButton find_btn, find_next, replace, replace_all, cancel;
Notepad samp;
public Find(Notepad mynote)
{
super("Find / Replace");
samp = mynote;
l1 = new Label("Find What: ");
l2 = new Label("Replace With: ");
tf = new TextField(30);
tr = new TextField(30);
find_btn = new JButton("Find");
find_next = new JButton("Find Next");
replace = new JButton("Replace");
replace_all = new JButton("Replace All");
cancel = new JButton("Cancel");
setLayout(null);
int label_w = 80;
int label_h = 20;
int tf_w = 120;
l1.setBounds(10,10, label_w, label_h);
add(l1);
tf.setBounds(10+label_w, 10, tf_w, 20);
add(tf);
l2.setBounds(10, 10+label_h+10, label_w, label_h);
add(l2);
tr.setBounds(10+label_w, 10+label_h+10, tf_w, 20);
add(tr);
find_btn.setBounds(220, 10, 100, 20);
add(find_btn);
find_btn.addActionListener(this);
find_next.setBounds(220, 30, 100, 20);
add(find_next);
find_next.addActionListener(this);
replace.setBounds(220, 50, 100, 20);
add(replace);
replace.addActionListener(this);
replace_all.setBounds(220, 70, 100, 20);
add(replace_all);
replace_all.addActionListener(this);
cancel.setBounds(220, 90, 100, 20);
add(cancel);
cancel.addActionListener(this);
int w = 340;
int h = 150;
setSize(w,h);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setLocation(center.x-w/2, center.y-h/2);
setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==find_btn)
{
find();
}
else if(e.getSource() == find_next)
{
find_next();
}
else if(e.getSource() == replace)
{
replace();
}
else if(e.getSource() == replace_all)
{
replace_all();
}
else if(e.getSource() == cancel)
{
this.setVisible(false);
}
}
public void find()
{
int select_start = samp.t.getText().indexOf(tf.getText());
if(select_start == -1)
{
startIndex = 0;
JOptionPane.showMessageDialog(null, "Could not find "+tf.getText()+"!");
return;
}
if(select_start == samp.t.getText().lastIndexOf(tf.getText()))
{
startIndex = 0;
}
int select_end = select_start+tf.getText().length();
samp.t.select(select_start, select_end);
}
public void find_next()
{
String selection = samp.t.getSelectedText();
try
{
selection.equals("");
}
catch(NullPointerException e)
{
selection = tf.getText();
try
{
selection.equals("");
}
catch(NullPointerException e2)
{
selection = JOptionPane.showInputDialog("Find:");
tf.setText(selection);
}
}
try
{
int select_start = samp.t.getText().indexOf(selection, startIndex);
int select_end = select_start+selection.length();
samp.t.select(select_start, select_end);
startIndex = select_end+1;
if(select_start == samp.t.getText().lastIndexOf(selection))
{
startIndex = 0;
}
}
catch(NullPointerException e)
{}
}
public void replace()
{
try
{
find();
samp.t.replaceSelection(tr.getText());
}
catch(NullPointerException e)
{
System.out.print("Null Pointer Exception: "+e);
}
}
public void replace_all()
{
samp.t.setText(samp.t.getText().replaceAll(tf.getText(), tr.getText()));
}
}
4. About.java. This file is all about me. This is optional file.
/* ***************
* Created By...
* Santanu Kar
* ****************/
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class About{
static JFrame window = new JFrame("About Notepad");
Notepad samp;
JButton btn;
public About(Notepad ref)
{
samp = ref;
Container c = window.getContentPane();
c.setLayout(new FlowLayout());
String about = "<html>" +
"<body>" +
"Created By...<br>" +
"Santanu Kar <br>" +
"Electronics & Communication Engineering<br>" +
"A.I.T.<br>" +
"W.B.U.T.<br><br><br>" +
"E-Mail: ask4santanu@gmail.com<br>" +
"Blog: http://thinksantanu.blogspot.in/<br><br>" +
"Version: 1.00<br>" +
"Built Date: June 4, 2013<br><br><br>" +
"</body>" +
"</html>";
JLabel l = new JLabel("", SwingConstants.LEFT);
l.setText(about);
l.setVerticalTextPosition(SwingConstants.TOP);
l.setIconTextGap(20);
c.add(l);
int w = 340;
int h = 250;
window.setSize(w,h);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
window.setLocation(center.x-w/2, center.y-h/2+25);
window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
window.setVisible(false);
window.setResizable(false);
}
}
5. Note.java. This file contains Main method.
/* ***************
* Created By...
* Santanu Kar
* ****************/
import javax.swing.JFrame;
public class Note {
public static void main(String args[]){
Notepad mynote = new Notepad();
mynote.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
N.B.:: To make an executable jar of this file, follow the steps below.
1. Compile all the .java files in the project folder.
2. Create a manifest.txt file and write in it, Main-Class: <main class name> and hit an enter. Save this file. Do not forget to place this file( manifest.txt) along-with(same path/folder) .class files.
3. Open command prompt, go to project path/folder from cmd. Then type:: jar cvfm filename.jar manifest.txt *.class
4. Double click to your newly created jar and start writing, editing.
** You must have jre in your machine to run the jar file.
No comments:
Post a Comment