-
Notifications
You must be signed in to change notification settings - Fork 17
/
Notepad.java
175 lines (138 loc) · 4.44 KB
/
Notepad.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
public class Notepad extends JFrame implements ActionListener, WindowListener {
JTextArea jta=new JTextArea();
File fnameContainer;
public Notepad(){
Font fnt=new Font("Arial",Font.PLAIN,15);
Container con=getContentPane();
JMenuBar jmb=new JMenuBar();
JMenu jmfile = new JMenu("File");
JMenu jmedit = new JMenu("Edit");
JMenu jmhelp = new JMenu("Help");
con.setLayout(new BorderLayout());
//trying to add scrollbar
JScrollPane sbrText = new JScrollPane(jta);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sbrText.setVisible(true);
jta.setFont(fnt);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
con.add(sbrText);
createMenuItem(jmfile,"New");
createMenuItem(jmfile,"Open");
createMenuItem(jmfile,"Save");
jmfile.addSeparator();
createMenuItem(jmfile,"Exit");
createMenuItem(jmedit,"Cut");
createMenuItem(jmedit,"Copy");
createMenuItem(jmedit,"Paste");
createMenuItem(jmhelp,"About Notepad");
jmb.add(jmfile);
jmb.add(jmedit);
jmb.add(jmhelp);
setJMenuBar(jmb);
setIconImage(Toolkit.getDefaultToolkit().getImage("notepad.gif"));
addWindowListener(this);
setSize(500,500);
setTitle("Untitled.txt - Notepad");
setVisible(true);
}
public void createMenuItem(JMenu jm,String txt){
JMenuItem jmi=new JMenuItem(txt);
jmi.addActionListener(this);
jm.add(jmi);
}
public void actionPerformed(ActionEvent e){
JFileChooser jfc=new JFileChooser();
if(e.getActionCommand().equals("New")){
//new
this.setTitle("Untitled.txt - Notepad");
jta.setText("");
fnameContainer=null;
}else if(e.getActionCommand().equals("Open")){
//open
int ret=jfc.showDialog(null,"Open");
if(ret == JFileChooser.APPROVE_OPTION)
{
try{
File fyl=jfc.getSelectedFile();
OpenFile(fyl.getAbsolutePath());
this.setTitle(fyl.getName()+ " - Notepad");
fnameContainer=fyl;
}catch(IOException ers){}
}
}else if(e.getActionCommand().equals("Save")){
//save
if(fnameContainer != null){
jfc.setCurrentDirectory(fnameContainer);
jfc.setSelectedFile(fnameContainer);
}
else {
//jfc.setCurrentDirectory(new File("."));
jfc.setSelectedFile(new File("Untitled.txt"));
}
int ret=jfc.showSaveDialog(null);
if(ret == JFileChooser.APPROVE_OPTION){
try{
File fyl=jfc.getSelectedFile();
SaveFile(fyl.getAbsolutePath());
this.setTitle(fyl.getName()+ " - Notepad");
fnameContainer=fyl;
}catch(Exception ers2){}
}
}else if(e.getActionCommand().equals("Exit")){
//exit
Exiting();
}else if(e.getActionCommand().equals("Copy")){
//copy
jta.copy();
}else if(e.getActionCommand().equals("Paste")){
//paste
jta.paste();
}else if(e.getActionCommand().equals("About Notepad")){
//about
JOptionPane.showMessageDialog(this,"Created by: Ferdinand Silva (http://ferdinandsilva.com)","Notepad",JOptionPane.INFORMATION_MESSAGE);
}else if(e.getActionCommand().equals("Cut")){
jta.cut();
}
}
public void OpenFile(String fname) throws IOException {
//open file code here
BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream(fname)));
String l;
//clear the textbox
jta.setText("");
setCursor(new Cursor(Cursor.WAIT_CURSOR));
while((l=d.readLine())!= null) {
jta.setText(jta.getText()+l+"\r\n");
}
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
d.close();
}
public void SaveFile(String fname) throws IOException {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
DataOutputStream o=new DataOutputStream(new FileOutputStream(fname));
o.writeBytes(jta.getText());
o.close();
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e) {
Exiting();
}
public void windowOpened(WindowEvent e){}
public void Exiting() {
System.exit(0);
}
public static void main (String[] args) {
Notepad notp=new Notepad();
}
}