TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Notepad in Java
rahul jaiswal
Sep 30, 2019
10.1
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
This is a sample code for Notepad in Java.
notepad.zip
Notepad In Java
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
);
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 =
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);
menubar.add(file);
menubar.add(edit);
menubar.add(format);
menubar.add(view);
menubar.add(help);
setJMenuBar(menubar);
new1 =
new
JMenuItem(
"New"
,
new
ImageIcon(
"new.jpg"
));
new1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
new1.addActionListener(
this
);
open =
new
JMenuItem(
"Open"
,
new
ImageIcon(
"open.jpg"
));
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
save =
new
JMenuItem(
"Save"
,
new
ImageIcon(
"save.jpg"
));
save.addActionListener(
this
);
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
saveas =
new
JMenuItem(
"Save as"
,
new
ImageIcon(
"saveas.jpg"
));
saveas.addActionListener(
this
);
pageset =
new
JMenuItem(
"Page Setup"
);
pageset.addActionListener(
this
);
print =
new
JMenuItem(
"Print"
,
new
ImageIcon(
"print.jpg"
));
print.addActionListener(
this
);
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);
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
);
word =
new
JCheckBoxMenuItem(
"Word Wrap"
);
format.add(word);
viewhelp =
new
JMenuItem(
"View Help"
,
new
ImageIcon(
"help.jpg"
));
abtnotepad =
new
JMenuItem(
"About Notepad"
);
help.add(viewhelp);
help.add(abtnotepad);
abtnotepad.addActionListener(
this
);
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.addSeparator();
format.add(xxx);
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
);
italic.addActionListener(
this
);
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
);
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();
}
}
Notepad
Next Recommended Reading
Two Sum Array Problem In Java