├── README.md ├── Colour.java ├── Format.java ├── File.java ├── Edit.java └── GUI.java /README.md: -------------------------------------------------------------------------------- 1 | # Notepad (TextEditor) 2 | ![image](https://user-images.githubusercontent.com/89564330/171341934-785b951b-0900-417c-9f79-651c934c33fd.png) 3 | 4 | --> Java based Notepad with Stack type clipboard to keep multiple strins in clipboard\ 5 | --> Graphical User Interface made by Java Swing\ 6 | --> A menu bar with follwing menus 7 | * 1. **File handling** (New , Open , Save, Save As , Exit), 8 | * 2. **Editing options** (Cut , Copy , Paste) 9 | * 3. **Formating of Fonts** with fontsize and font type with Word Wrap option 10 | * 4. **Background colour** change 11 | -------------------------------------------------------------------------------- /Colour.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | 3 | public class Colour { 4 | GUI gui; 5 | 6 | public Colour(GUI gui){ 7 | this.gui = gui; 8 | } 9 | 10 | public void changeColour(String col){ 11 | 12 | switch (col) { 13 | 14 | case "White": gui.window.getContentPane().setBackground(Color.white); 15 | gui.textArea.setBackground(Color.white); 16 | gui.textArea.setForeground(Color.black); 17 | break; 18 | 19 | case "Black": gui.window.getContentPane().setBackground(Color.black); 20 | gui.textArea.setBackground(Color.black); 21 | gui.textArea.setForeground(Color.white); 22 | break; 23 | 24 | case "Blue": gui.window.getContentPane().setBackground(Color.blue); 25 | gui.textArea.setBackground(Color.blue); 26 | gui.textArea.setForeground(Color.white); 27 | break; 28 | 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Format.java: -------------------------------------------------------------------------------- 1 | import java.awt.Font; 2 | 3 | public class Format { 4 | GUI gui; 5 | Font arial , comicSansMS , timesNewRoman; 6 | String selectedFont; 7 | 8 | public Format(GUI gui){ 9 | this.gui = gui; 10 | } 11 | 12 | public void wordWrap(){ 13 | 14 | if(!gui.wordWrapOn){ 15 | gui.wordWrapOn = true; 16 | gui.textArea.setLineWrap(true); 17 | gui.textArea.setWrapStyleWord(true); 18 | gui.iWrap.setText("Word Wrap : ON"); 19 | }else{ 20 | gui.wordWrapOn = false; 21 | gui.textArea.setLineWrap(false); 22 | gui.textArea.setWrapStyleWord(false); 23 | gui.iWrap.setText("Word Wrap : OFF"); 24 | } 25 | } 26 | 27 | public void createFont(int fontSize){ 28 | 29 | arial = new Font("Arial" , Font.PLAIN , fontSize); 30 | comicSansMS = new Font("Comic Sans MS" , Font.PLAIN , fontSize); 31 | timesNewRoman = new Font("Times New Roman" , Font.PLAIN , fontSize); 32 | 33 | setFont(selectedFont); 34 | } 35 | 36 | public void setFont(String font){ 37 | selectedFont = font; 38 | 39 | switch (selectedFont) { 40 | case "Arial": gui.textArea.setFont(arial); 41 | break; 42 | case "Comic Sans MS" : gui.textArea.setFont(comicSansMS); 43 | break; 44 | case "Times New Roman" : gui.textArea.setFont(timesNewRoman); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /File.java: -------------------------------------------------------------------------------- 1 | import java.awt.FileDialog; 2 | import java.io.BufferedReader; 3 | import java.io.FileReader; 4 | import java.io.FileWriter; 5 | 6 | public class File { 7 | GUI gui; 8 | String fileName; 9 | String fileAddress; 10 | 11 | public File(GUI gui){ 12 | this.gui = gui; 13 | } 14 | 15 | public File(String string) { 16 | } 17 | 18 | public void newFile(){ /* New File */ 19 | gui.textArea.setText(""); //Create Empty new file 20 | gui.window.setTitle("New"); 21 | fileName = null; 22 | fileAddress = null; 23 | } 24 | 25 | public void open(){ /* Open */ 26 | FileDialog fd = new FileDialog(gui.window , "Open" , FileDialog.LOAD); //Load a pop up for file path 27 | fd.setVisible(true); 28 | 29 | if(fd.getFile() != null){ 30 | fileName = fd.getFile(); 31 | fileAddress = fd.getDirectory(); 32 | gui.window.setTitle(fileName); 33 | } 34 | 35 | try { 36 | //BufferedReader = A class which reads text from a character-input stream 37 | BufferedReader br = new BufferedReader(new FileReader(fileAddress + fileName)); //An Address to read file 38 | 39 | gui.textArea.setText(""); 40 | 41 | String line = null; 42 | 43 | while((line = br.readLine()) != null){ 44 | gui.textArea.append(line + "\n"); 45 | } 46 | br.close(); 47 | 48 | } catch (Exception e) { 49 | System.out.println("File Not Opened!!!"); 50 | } 51 | } 52 | 53 | public void save(){ /* Save */ 54 | 55 | if(fileName == null){ 56 | saveAs(); 57 | 58 | }else{ 59 | try { 60 | FileWriter fw = new FileWriter(fileAddress + fileName); 61 | fw.write(gui.textArea.getText()); //Write file 62 | gui.window.setTitle(fileName); //File name 63 | fw.close(); 64 | 65 | } catch (Exception e) { 66 | System.out.println("Something wrong happened!!!"); 67 | } 68 | } 69 | } 70 | 71 | public void saveAs(){ /* Save As */ 72 | 73 | FileDialog fd = new FileDialog(gui.window , "Save" , FileDialog.SAVE); 74 | fd.setVisible(true); 75 | 76 | if(fd.getFile() != null){ 77 | fileName = fd.getFile(); 78 | fileAddress = fd.getDirectory(); 79 | gui.window.setTitle(fileName); //File name 80 | } 81 | 82 | try { 83 | FileWriter fw = new FileWriter(fileAddress + fileName); 84 | fw.write(gui.textArea.getText()); //Write file 85 | fw.close(); 86 | 87 | } catch (Exception e) { 88 | System.out.println("Something wrong happened!!!"); 89 | } 90 | } 91 | 92 | public void exit(){ 93 | System.exit(0); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Edit.java: -------------------------------------------------------------------------------- 1 | import java.util.Calendar; 2 | import java.util.GregorianCalendar; 3 | 4 | public class Edit { 5 | GUI gui; 6 | public Edit(GUI gui){ 7 | this.gui = gui; 8 | } 9 | 10 | public void editText(String command){ 11 | switch (command) { 12 | case "Copy to Stack": gui.stack.push(gui.textArea.getSelectedText()); 13 | break; 14 | 15 | case "Copy (Ctrl + C)" : gui.str = gui.textArea.getSelectedText(); 16 | break; 17 | 18 | case "Cut to Stack" : gui.stack.push(gui.textArea.getSelectedText()); 19 | gui.i = gui.textArea.getText().indexOf(gui.str); 20 | gui.textArea.replaceRange("", gui.i, gui.i + gui.str.length()); 21 | break; 22 | 23 | case "Cut (Ctrl + X)" : gui.str = gui.textArea.getSelectedText(); 24 | gui.i = gui.textArea.getText().indexOf(gui.str); 25 | gui.textArea.replaceRange("", gui.i, gui.str.length()); 26 | break; 27 | 28 | case "Paste from Stack" : gui.pos = gui.textArea.getCaretPosition(); //Get Position of Cursor 29 | try { 30 | gui.textArea.insert((String)gui.stack.pop(), gui.pos); 31 | } catch (Exception e) { 32 | gui.textArea.insert("", gui.pos); 33 | } 34 | break; 35 | 36 | case "Paste (Ctrl + V)" : gui.pos = gui.textArea.getCaretPosition(); 37 | gui.textArea.insert(gui.str, gui.pos); 38 | 39 | case "Time & Date" : gui.gcalendar = new GregorianCalendar(); 40 | String hour = String.valueOf(gui.gcalendar.get(Calendar.HOUR)); 41 | String min = String.valueOf(gui.gcalendar.get(Calendar.MINUTE)); 42 | String sec = String.valueOf(gui.gcalendar.get(Calendar.SECOND)); 43 | String date = String.valueOf(gui.gcalendar.get(Calendar.DATE)); 44 | String month = String.valueOf(gui.gcalendar.get(Calendar.MONTH)); 45 | String year = String.valueOf(gui.gcalendar.get(Calendar.YEAR)); 46 | 47 | if (Integer.parseInt(hour) < 10) 48 | hour = "0" + hour; 49 | 50 | if (Integer.parseInt(min) < 10) 51 | min = "0" + min; 52 | 53 | if (Integer.parseInt(sec) < 10) 54 | sec = "0" + sec; 55 | 56 | if (Integer.parseInt(date) < 10) 57 | date = "0" + date; 58 | 59 | if (Integer.parseInt(month) < 10) 60 | month = "0" + month; 61 | 62 | if (Integer.parseInt(year) < 10) 63 | year = "0" + year; 64 | 65 | String total = "Time :- " + hour + ":" + min + ":" + sec + " Date :- " + date + "/" + month + "/" + year; 66 | int loc = gui.textArea.getCaretPosition(); 67 | gui.textArea.insert(total, loc); 68 | break; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /GUI.java: -------------------------------------------------------------------------------- 1 | import java.awt.event.ActionEvent; 2 | import java.awt.event.ActionListener; 3 | import java.util.GregorianCalendar; 4 | import java.util.Stack; 5 | 6 | import javax.swing.BorderFactory; 7 | import javax.swing.JFrame; 8 | import javax.swing.JMenu; 9 | import javax.swing.JMenuBar; 10 | import javax.swing.JMenuItem; 11 | import javax.swing.JScrollPane; 12 | import javax.swing.JTextArea; 13 | 14 | public class GUI implements ActionListener{ 15 | 16 | /* JFrame works like the main Windoow where components like lables, buttons ,textfields are added to create a GUI */ 17 | /* Text Area */ 18 | JFrame window; 19 | JTextArea textArea; //Multi-line area that displays plain text 20 | JScrollPane scrollPane; //Add Scrollbar when we enter beyound text area 21 | boolean wordWrapOn = false; 22 | 23 | /* Top Menu Bar */ 24 | JMenuBar menuBar; //Add Menu bar of Notepad(File, Edit , Format , Colour etc) 25 | JMenu menuFile, menuEdit , menuFormat , menuColour; //Menu icons 26 | 27 | /* File Menu */ 28 | JMenuItem iNew , iOpen , iSave , iSaveAs, iExit; //Menu Items 29 | 30 | /* Format Menu */ 31 | JMenuItem iWrap, iFontArial , iFontCSMS , iFontTNR , iFontSize8, iFontSize12 , iFontSize16 , iFontSize20 , iFontSize24 , iFontSize28; 32 | JMenu menuFont , menuFontSize; 33 | 34 | /* Edit Menu */ 35 | JMenuItem iCopy ,iCopy1 , iCut1 , iCut , iPaste1 ,iPaste , iDelete , iTD, iAllSel ; 36 | Stack stack = new Stack<>(); 37 | String str = ""; 38 | int i=0, pos = 0; 39 | GregorianCalendar gcalendar; 40 | 41 | /* Colour Menu */ 42 | JMenuItem iCol1 , iCol2 , iCol3; 43 | 44 | File fun = new File(this); 45 | Format format = new Format(this); 46 | Edit edit = new Edit(this); 47 | Colour col = new Colour(this); 48 | 49 | public static void main(String[] args) { 50 | new GUI(); 51 | 52 | } 53 | 54 | public GUI(){ 55 | createWindow(); 56 | createTextArea(); 57 | createMenuBar(); 58 | createFileMenu(); 59 | createEdit(); 60 | createFormatMenu(); 61 | createColourMenu(); 62 | 63 | format.selectedFont = "Arial"; 64 | format.createFont(12); 65 | format.wordWrap(); 66 | col.changeColour("White"); 67 | window.setVisible(true); //We can see window 68 | } 69 | 70 | public void createWindow(){ 71 | window = new JFrame("Notepad"); 72 | window.setSize(800, 600); 73 | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //we need to close frame properly 74 | } 75 | 76 | public void createTextArea(){ 77 | textArea = new JTextArea(); 78 | scrollPane = new JScrollPane(textArea , JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED , JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 79 | scrollPane.setBorder(BorderFactory.createEmptyBorder()); //Remove borders of Scroll Bar 80 | window.add(scrollPane); 81 | 82 | } 83 | 84 | public void createMenuBar(){ 85 | menuBar = new JMenuBar(); 86 | window.setJMenuBar(menuBar); 87 | 88 | menuFile = new JMenu("File"); 89 | menuBar.add(menuFile); 90 | 91 | menuEdit = new JMenu("Edit"); 92 | menuBar.add(menuEdit); 93 | 94 | menuFormat = new JMenu("Format"); 95 | menuBar.add(menuFormat); 96 | 97 | menuColour = new JMenu("Background Colour"); 98 | menuBar.add(menuColour); 99 | } 100 | 101 | public void createFileMenu(){ 102 | iNew = new JMenuItem("New"); 103 | iNew.addActionListener(this); //we're implementing Action Listner to this GUI class 104 | iNew.setActionCommand("New"); //set string value to trigger the ActionListener on this item 105 | menuFile.add(iNew); 106 | 107 | iOpen = new JMenuItem("Open"); 108 | iOpen.addActionListener(this); 109 | iOpen.setActionCommand("Open"); 110 | menuFile.add(iOpen); 111 | 112 | iSave = new JMenuItem("Save"); 113 | iSave.addActionListener(this); 114 | iSave.setActionCommand("Save"); 115 | menuFile.add(iSave); 116 | 117 | iSaveAs = new JMenuItem("Save As"); 118 | iSaveAs.addActionListener(this); 119 | iSaveAs.setActionCommand("SaveAs"); 120 | menuFile.add(iSaveAs); 121 | 122 | iExit = new JMenuItem("Exit"); 123 | iExit.addActionListener(this); 124 | iExit.setActionCommand("Exit"); 125 | menuFile.add(iExit); 126 | } 127 | 128 | public void createEdit(){ 129 | iCopy = new JMenuItem("Copy to Stack"); 130 | iCopy.addActionListener(this); 131 | iCopy.setActionCommand("Copy to Stack"); 132 | menuEdit.add(iCopy); 133 | 134 | iCut = new JMenuItem("Cut to Stack"); 135 | iCut.addActionListener(this); 136 | iCut.setActionCommand("Cut to Stack"); 137 | menuEdit.add(iCut); 138 | 139 | iPaste = new JMenuItem("Paste from Stack"); 140 | iPaste.addActionListener(this); 141 | iPaste.setActionCommand("Paste from Stack"); 142 | menuEdit.add(iPaste); 143 | 144 | iCopy1 = new JMenuItem("Copy (Ctrl + C)"); 145 | iCopy1.addActionListener(this); 146 | iCopy1.setActionCommand("Copy (Ctrl + C)"); 147 | menuEdit.add(iCopy1); 148 | 149 | iCut1 = new JMenuItem("Cut (Ctrl + X)"); 150 | iCut1.addActionListener(this); 151 | iCut1.setActionCommand("Cut (Ctrl + X)"); 152 | menuEdit.add(iCut1); 153 | 154 | iPaste1 = new JMenuItem("Paste (Ctrl + V)"); 155 | iPaste1.addActionListener(this); 156 | iPaste1.setActionCommand("Paste (Ctrl + V)"); 157 | menuEdit.add(iPaste1); 158 | 159 | iTD = new JMenuItem("Time & Date"); 160 | iTD.addActionListener(this); 161 | iTD.setActionCommand("Time & Date"); 162 | menuEdit.add(iTD); 163 | } 164 | 165 | public void createFormatMenu(){ 166 | 167 | iWrap = new JMenuItem("Word Wrap : OFF"); 168 | iWrap.addActionListener(this); 169 | iWrap.setActionCommand("Word Wrap"); 170 | menuFormat.add(iWrap); 171 | 172 | menuFont = new JMenu("Font"); 173 | menuFormat.add(menuFont); 174 | 175 | iFontArial = new JMenuItem("Arial"); 176 | iFontArial.addActionListener(this); 177 | iFontArial.setActionCommand("Arial"); 178 | menuFont.add(iFontArial); 179 | 180 | 181 | iFontCSMS = new JMenuItem("Comic Sans MS"); 182 | iFontCSMS.addActionListener(this); 183 | iFontCSMS.setActionCommand("Comic Sans MS"); 184 | menuFont.add(iFontCSMS); 185 | 186 | iFontTNR = new JMenuItem("Times New Roman"); 187 | iFontTNR.addActionListener(this); 188 | iFontTNR.setActionCommand("Times New Roman"); 189 | menuFont.add(iFontTNR); 190 | 191 | menuFontSize = new JMenu("Font Size"); 192 | menuFormat.add(menuFontSize); 193 | 194 | iFontSize8 = new JMenuItem("8"); 195 | iFontSize8.addActionListener(this); 196 | iFontSize8.setActionCommand("size8"); 197 | menuFontSize.add(iFontSize8); 198 | 199 | iFontSize12 = new JMenuItem("12"); 200 | iFontSize12.addActionListener(this); 201 | iFontSize12.setActionCommand("size12"); 202 | menuFontSize.add(iFontSize12); 203 | 204 | iFontSize16 = new JMenuItem("16"); 205 | iFontSize16.addActionListener(this); 206 | iFontSize16.setActionCommand("size16"); 207 | menuFontSize.add(iFontSize16); 208 | 209 | iFontSize20 = new JMenuItem("20"); 210 | iFontSize20.addActionListener(this); 211 | iFontSize20.setActionCommand("size20"); 212 | menuFontSize.add(iFontSize20); 213 | 214 | iFontSize24 = new JMenuItem("24"); 215 | iFontSize24.addActionListener(this); 216 | iFontSize24.setActionCommand("size24"); 217 | menuFontSize.add(iFontSize24); 218 | 219 | iFontSize28 = new JMenuItem("28"); 220 | iFontSize28.addActionListener(this); 221 | iFontSize28.setActionCommand("size28"); 222 | menuFontSize.add(iFontSize28); 223 | } 224 | 225 | public void createColourMenu(){ 226 | iCol1 = new JMenuItem("White"); 227 | iCol1.addActionListener(this); 228 | iCol1.setActionCommand("White"); 229 | menuColour.add(iCol1); 230 | 231 | iCol2 = new JMenuItem("Black"); 232 | iCol2.addActionListener(this); 233 | iCol2.setActionCommand("Black"); 234 | menuColour.add(iCol2); 235 | 236 | iCol3 = new JMenuItem("Blue"); 237 | iCol3.addActionListener(this); 238 | iCol3.setActionCommand("Blue"); 239 | menuColour.add(iCol3); 240 | } 241 | 242 | @Override 243 | public void actionPerformed(ActionEvent e){ 244 | String command = e.getActionCommand(); 245 | 246 | switch(command){ 247 | case "New" : fun.newFile(); 248 | break; 249 | case "Open" : fun.open(); 250 | break; 251 | case "SaveAs" : fun.saveAs(); 252 | break; 253 | case "Save" : fun.save(); 254 | break; 255 | case "Exit" : fun.exit(); 256 | break; 257 | case "Word Wrap" : format.wordWrap(); 258 | break; 259 | case "Arial" : format.setFont(command); 260 | break; 261 | case "Comic Sans MS": format.setFont(command); 262 | break; 263 | case "Times New Roman": format.setFont(command); 264 | break; 265 | case "size8" : format.createFont(8); 266 | break; 267 | case "size12" : format.createFont(12); 268 | break; 269 | case "size16" : format.createFont(16); 270 | break; 271 | case "size20" : format.createFont(20); 272 | break; 273 | case "size24" : format.createFont(24); 274 | break; 275 | case "size28" : format.createFont(28); 276 | break; 277 | case "White" : col.changeColour(command); 278 | break; 279 | case "Black" : col.changeColour(command); 280 | break; 281 | case "Blue" : col.changeColour(command); 282 | break; 283 | case "Copy to Stack" : edit.editText(command); 284 | break; 285 | case "Copy (Ctrl + C)" : edit.editText(command); 286 | break; 287 | case "Cut to Stack" : edit.editText(command); 288 | break; 289 | case "Cut (Ctrl + X)" : edit.editText(command); 290 | break; 291 | case "Paste from Stack" : edit.editText(command); 292 | break; 293 | case "Paste (Ctrl + V)" : edit.editText(command); 294 | break; 295 | case "Time & Date" : edit.editText(command); 296 | break; 297 | } 298 | } 299 | } 300 | --------------------------------------------------------------------------------