forked from satruddh/Hacktoberfest2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyNotepad.java
413 lines (390 loc) · 13.6 KB
/
MyNotepad.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.swing.filechooser.*;
/**
*
* @author SPN RAO
*/
public class MyNotepad extends javax.swing.JFrame
{
private javax.swing.JPanel panel;
private javax.swing.JTextArea area;
private javax.swing.JScrollPane scroll;
private javax.swing.JLabel fnl;
private javax.swing.JMenuBar menubar;
private javax.swing.JMenu file;
private javax.swing.JMenu edit;
private javax.swing.JMenu view;
private javax.swing.JMenu lnf;
private javax.swing.JMenu help;
private javax.swing.JMenuItem close;
private javax.swing.JMenuItem open;
private javax.swing.JMenuItem save;
private javax.swing.JMenuItem saveas;
private javax.swing.JMenuItem clear;
private javax.swing.JMenuItem windows;
private javax.swing.JMenuItem metal;
private javax.swing.JMenuItem motif;
private javax.swing.JMenuItem nimbus;
private javax.swing.JMenuItem about;
private javax.swing.JCheckBoxMenuItem isEdi;
private javax.swing.JCheckBoxMenuItem txtarea;
private javax.swing.JPopupMenu.Separator sep1;
private javax.swing.JPopupMenu.Separator sep2;
public MyNotepad()
{
initComponents();
}
private void initComponents()
{
panel=new javax.swing.JPanel();
area=new javax.swing.JTextArea();
scroll=new javax.swing.JScrollPane();
menubar = new javax.swing.JMenuBar();
fnl= new javax.swing.JLabel("");
file = new javax.swing.JMenu();
close=new javax.swing.JMenuItem();
open=new javax.swing.JMenuItem();
save=new javax.swing.JMenuItem();
saveas =new javax.swing.JMenuItem();
edit=new javax.swing.JMenu();
isEdi =new javax.swing.JCheckBoxMenuItem();
clear=new javax.swing.JMenuItem();
view =new javax.swing.JMenu();
txtarea=new javax.swing.JCheckBoxMenuItem();
lnf=new javax.swing.JMenu();
windows =new javax.swing.JMenuItem();
metal = new javax.swing.JMenuItem();
motif = new javax.swing.JMenuItem();
nimbus = new javax.swing.JMenuItem();
help=new javax.swing.JMenu();
about = new javax.swing.JMenuItem();
sep1 = new javax.swing.JPopupMenu.Separator();
sep1 = new javax.swing.JPopupMenu.Separator();
panel.setLayout(new BorderLayout());
file.setText("File");
file.setMnemonic(KeyEvent.VK_F);
close.setText("Close");
close.setAccelerator(javax.swing.KeyStroke.getKeyStroke(KeyEvent.VK_X,java.awt.event.InputEvent.CTRL_DOWN_MASK));
close.setToolTipText("Exit application");
close.addActionListener((ActionEvent event) -> {
closeActionPerformed(event);
});
open.setText("Open...");
open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.event.InputEvent.CTRL_DOWN_MASK));
open.setToolTipText("Open Files");
open.addActionListener((ActionEvent ev) -> {
openActionPerformed(ev);
});
save.setText("Save");
save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(KeyEvent.VK_S,java.awt.event.InputEvent.CTRL_DOWN_MASK));
save.addActionListener((ActionEvent evt) -> {
saveActionPerformed(evt);
});
saveas.setText("Save As...");
saveas.setAccelerator(javax.swing.KeyStroke.getKeyStroke(KeyEvent.VK_S,java.awt.event.InputEvent.SHIFT_DOWN_MASK | java.awt.event.InputEvent.CTRL_DOWN_MASK));
saveas.addActionListener((ActionEvent evt) -> {
saveasActionPerformed(evt);
});
file.add(open);
file.addSeparator();
file.add(save);
file.add(saveas);
file.addSeparator();
file.add(close);
menubar.add(file);
edit.setText("Edit");
clear.setText("Clear");
clear.addActionListener((ActionEvent e) -> {
clearActionPerformed(e);
});
isEdi.setText("Set Editable");
isEdi.setState(true);
isEdi.addActionListener((ActionEvent evt) -> {
isEdiActionPerformed(evt);
});
edit.add(clear);
edit.add(isEdi);
menubar.add(edit);
view.setText("View");
txtarea.setText("Show Text Area");
txtarea.setState(area.isVisible());
txtarea.addActionListener((ActionEvent evt) -> {
txtareaActionPerformed(evt);
});
lnf.setText("Look And Feel");
windows.setText("Windows");
windows.addActionListener((ActionEvent evt) -> {
windowsActionPerformed(evt);
});
lnf.add(windows);
metal.setText("Metal");
metal.addActionListener((ActionEvent ev) -> {
metalActionPerformed(ev);
});
lnf.add(metal);
motif.setText("Motif");
motif.addActionListener((ActionEvent ev) -> {
motifActionPerformed(ev);
});
lnf.add(motif);
nimbus.setText("Nimbus");
nimbus.addActionListener((ActionEvent ev) -> {
nimbusActionPerformed(ev);
});
lnf.add(nimbus);
view.add(lnf);
view.add(txtarea);
menubar.add(view);
help.setText("Help");
about.setText("About...");
about.addActionListener((ActionEvent evt) -> {
aboutActionPerformed(evt);
});
help.add(about);
menubar.add(help);
area.setBorder(javax.swing.BorderFactory.createEtchedBorder(10,java.awt.Color.BLUE,java.awt.Color.GREEN));
area.setEditable(true);
area.setTabSize(4);
scroll.getViewport().add(area);
panel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,10,0));
panel.add(scroll);
setTitle("Sample Notepad");
setJMenuBar(menubar);
setSize(new java.awt.Dimension(900, 600));
add(panel);
setLocationRelativeTo(null);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
setFocusable(true);
}
private void closeActionPerformed(ActionEvent evt){
System.exit(0);
}
private void openActionPerformed(ActionEvent evt)
{
javax.swing.JFileChooser cf=new javax.swing.JFileChooser();
FileFilter jv=new FileNameExtensionFilter("JAVA file","java");
FileFilter js=new FileNameExtensionFilter("JavaScript File","js");
cf.addChoosableFileFilter(jv);
cf.addChoosableFileFilter(js);
int ret=cf.showDialog(panel,"OpenFile");
if(ret ==javax.swing.JFileChooser.APPROVE_OPTION)
{
File opf=cf.getSelectedFile();
fnl.setText(""+cf.getSelectedFile());
String text=readFile(opf);
area.setText(text);
setTitle(fnl.getText().substring(fnl.getText().lastIndexOf('\\')+1)+" - Sample Notepad");
area.setVisible(true);
}
else if(ret==javax.swing.JFileChooser.CANCEL_OPTION)
setTitle("Blank - Sample Notepad");
}
private void saveActionPerformed(ActionEvent evt)
{
String filename=fnl.getText();
if(filename.contains("\\"))
{
try
{
FileOutputStream un=new FileOutputStream(new File(filename));
DataOutputStream wr=new DataOutputStream(un);
wr.writeBytes(area.getText());
wr.flush();
wr.close();
}
catch( IOException ioe)
{
ioe.printStackTrace();
}
}
else
{
Save_as();
}
}
private void saveasActionPerformed(ActionEvent evt)
{
Save_as();
}
private void clearActionPerformed(ActionEvent evt)
{
area.setText("");
}
private void isEdiActionPerformed(ActionEvent evt)
{
if(!(isEdi.getState()))
{
area.setEditable(false);
}
else
{
area.setEditable(true);
}
}
private void txtareaActionPerformed(ActionEvent evt)
{
if(area.isVisible())
{
area.setVisible(false);
txtarea.setState(false);
}
else
{
area.setVisible(true);
txtarea.setVisible(true);
}
}
private void windowsActionPerformed(ActionEvent evt)
{
try
{
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
catch(ClassNotFoundException e)
{
System.out.println("LookAndFeel Class Not Found");
}
catch(IllegalAccessException e)
{
System.out.println("Access Denined");
}
catch(InstantiationException e)
{
System.out.println("Could Not Load LookAndFeel Class");
}
catch(javax.swing.UnsupportedLookAndFeelException e)
{
System.out.println("LookAndFeel is not supported");
}
}
private void motifActionPerformed(ActionEvent evt)
{
try
{
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
catch(ClassNotFoundException e)
{
System.out.println("LookAndFeel Class Not Found");
}
catch(IllegalAccessException e)
{
System.out.println("Access Denined");
}
catch(InstantiationException e)
{
System.out.println("Could Not Load LookAndFeel Class");
}
catch(javax.swing.UnsupportedLookAndFeelException e)
{
System.out.println("LookAndFeel is not supported");
}
}
private void metalActionPerformed(ActionEvent evt)
{
try
{
javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
catch(ClassNotFoundException e)
{
System.out.println("LookAndFeel Class Not Found");
}
catch(IllegalAccessException e)
{
System.out.println("Access Denined");
}
catch(InstantiationException e)
{
System.out.println("Could Not Load LookAndFeel Class");
}
catch(javax.swing.UnsupportedLookAndFeelException e)
{
System.out.println("LookAndFeel is not supported");
}
}
private void nimbusActionPerformed(ActionEvent evt)
{
try
{
javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
javax.swing.SwingUtilities.updateComponentTreeUI(this);
}
catch(ClassNotFoundException e)
{
System.out.println("LookAndFeel Class Not Found");
}
catch(IllegalAccessException e)
{
System.out.println("Access Denined");
}
catch(InstantiationException e)
{
System.out.println("Could Not Load LookAndFeel Class");
}
catch(javax.swing.UnsupportedLookAndFeelException e)
{
System.out.println("LookAndFeel is not supported");
}
}
private void aboutActionPerformed(ActionEvent evt)
{
javax.swing.JOptionPane.showMessageDialog(null,"By:- Surya Pratap\nEmail:- spnrao2013@gmail.com\n\n \u00A9 All Rights Reserved.\n v 1.0","About...",javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
public String readFile(File f1)
{
StringBuffer fileBuffer=null;
String fileString=null;
String line=null;
try{
FileReader in=new FileReader(f1);
BufferedReader brd=new BufferedReader(in);
fileBuffer=new StringBuffer();
while((line=brd.readLine())!=null)
{
fileBuffer.append(line + System.getProperty("line.separator"));
}
in.close();
fileString=fileBuffer.toString();
}
catch(IOException e)
{
e.printStackTrace();
}
return fileString;
}
public void Save_as()
{
FileDialog fd = new FileDialog(new javax.swing.JFrame(), "Save File", FileDialog.SAVE);
fd.show();
fnl.setText(""+fd.getDirectory()+fd.getFile()+".txt");
try
{
DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(fnl.getText())));
dos.writeBytes(area.getText());
dos.flush();
dos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(() -> {
new MyNotepad().setVisible(true);
});
}
}