Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Core Java Code Examples

Notepad

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
Notepad import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; public class Editor extends Frame { String filename; TextArea tx; Clipboard clip = getToolkit().getSystemClipboard(); Editor() { setLayout(new GridLayout(1,1)); tx = new TextArea(); add(tx); MenuBar mb = new MenuBar(); Menu F = new Menu("file"); MenuItem n = new MenuItem("New"); MenuItem o = new MenuItem("Open"); MenuItem s = new MenuItem("Save"); MenuItem e = new MenuItem("Exit"); n.addActionListener(new New()); F.add(n); o.addActionListener(new Open()); F.add(o); s.addActionListener(new Save()); F.add(s); e.addActionListener(new Exit()); F.add(e); mb.add(F); Menu E = new Menu("Edit"); MenuItem cut = new MenuItem("Cut"); MenuItem copy = new MenuItem("Copy"); MenuItem paste = new MenuItem("Paste"); cut.addActionListener(new Cut()); E.add(cut); copy.addActionListener(new Copy()); E.add(copy); paste.addActionListener(new Paste()); E.add(paste); mb.add(E); setMenuBar(mb); mylistener mylist = new mylistener(); addWindowListener(mylist); } class mylistener extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } class New implements ActionListener { public void actionPerformed(ActionEvent e) { tx.setText(" "); setTitle(filename); } } class Open implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); ReadFile(); } tx.requestFocus(); } } class Save implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); try { DataOutputStream d = new DataOutputStream(new FileOutputStream(filename)); String line = tx.getText(); BufferedReader br = new BufferedReader(new StringReader(line)); while((line = br.readLine())!=null) { d.writeBytes(line + "\r\n"); d.close(); } } catch(Exception ex) { System.out.println("File not found"); } tx.requestFocus(); } } } class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } void ReadFile() { BufferedReader d; StringBuffer sb = new StringBuffer(); try { d = new BufferedReader(new FileReader(filename)); String line; while((line=d.readLine())!=null) sb.append(line + "\n"); tx.setText(sb.toString()); d.close(); } catch(FileNotFoundException fe) { System.out.println("File not Found"); } catch(IOException ioe){} } class Cut implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection ss = new StringSelection(sel); clip.setContents(ss,ss); tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd()); } } class Copy implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection clipString = new StringSelection(sel); clip.setContents(clipString,clipString); } } class Paste implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable cliptran = clip.getContents(Editor.this); try { String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor); tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd()); } catch(Exception exc) { System.out.println("not string flavour"); } } } public static void main(String args[]) { Frame f = new Editor(); f.setSize(500,400); f.setVisible(true); f.show(); } }