|
| 1 | +// Java Program to create a text editor using java |
| 2 | +import java.awt.*; |
| 3 | +import javax.swing.*; |
| 4 | +import java.io.*; |
| 5 | +import java.awt.event.*; |
| 6 | +import javax.swing.plaf.metal.*; |
| 7 | +import javax.swing.text.*; |
| 8 | + |
| 9 | +import javafx.stage.Modality; |
| 10 | +class editor extends JFrame implements ActionListener { |
| 11 | + // Text component |
| 12 | + JTextArea t; |
| 13 | + |
| 14 | + // Frame |
| 15 | + JFrame f; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + // Constructor |
| 20 | + editor() |
| 21 | + { |
| 22 | + |
| 23 | + |
| 24 | + f = new JFrame("Borax Code"); |
| 25 | + |
| 26 | + ImageIcon img = new ImageIcon("text-editor-icon.png"); |
| 27 | + f.setIconImage(img.getImage()); |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + try { |
| 32 | + // Set metl look and feel |
| 33 | + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //javax.swing.plaf.windows.WindowsLookAndFeel |
| 34 | + |
| 35 | + } |
| 36 | + catch (Exception e) { |
| 37 | + } |
| 38 | + |
| 39 | + // Text component |
| 40 | + |
| 41 | + t = new JTextArea(); |
| 42 | + |
| 43 | + t.setFont(t.getFont().deriveFont(20f)); |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + JScrollPane scroll = new JScrollPane (t); |
| 48 | + scroll.setPreferredSize(t.getPreferredSize()); |
| 49 | + scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ); |
| 50 | + |
| 51 | + |
| 52 | + // Create a menubar |
| 53 | + JMenuBar mb = new JMenuBar(); |
| 54 | + |
| 55 | + // Create a menu for menu |
| 56 | + JMenu m1 = new JMenu("File"); |
| 57 | + |
| 58 | + m1.setPreferredSize(new Dimension(80, m1.getPreferredSize().height)); |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + // Create menu items |
| 63 | + JMenuItem mi1 = new JMenuItem("New"); |
| 64 | + JMenuItem mi2 = new JMenuItem("Open"); |
| 65 | + JMenuItem mi3 = new JMenuItem("Save"); |
| 66 | + JMenuItem mi9 = new JMenuItem("Print"); |
| 67 | + |
| 68 | + |
| 69 | + mb.add(Box.createVerticalGlue()); |
| 70 | + |
| 71 | + |
| 72 | + // Add action listener |
| 73 | + mi1.addActionListener(this); |
| 74 | + mi2.addActionListener(this); |
| 75 | + mi3.addActionListener(this); |
| 76 | + mi9.addActionListener(this); |
| 77 | + |
| 78 | + m1.add(mi1); |
| 79 | + m1.add(mi2); |
| 80 | + m1.add(mi3); |
| 81 | + m1.add(mi9); |
| 82 | + |
| 83 | + // Create amenu for menu |
| 84 | + JMenu m2 = new JMenu("Edit"); |
| 85 | + m2.setPreferredSize(new Dimension(80, m2.getPreferredSize().height)); |
| 86 | + |
| 87 | + |
| 88 | + // Create menu items |
| 89 | + JMenuItem mi4 = new JMenuItem("cut"); |
| 90 | + JMenuItem mi5 = new JMenuItem("copy"); |
| 91 | + JMenuItem mi6 = new JMenuItem("paste"); |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + // Add action listener |
| 98 | + mi4.addActionListener(this); |
| 99 | + mi5.addActionListener(this); |
| 100 | + mi6.addActionListener(this); |
| 101 | + |
| 102 | + m2.add(mi4); |
| 103 | + m2.add(mi5); |
| 104 | + m2.add(mi6); |
| 105 | + |
| 106 | + JMenuItem mc = new JMenuItem("Close"); |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + mc.addActionListener(this); |
| 112 | + |
| 113 | + mb.add(m1); |
| 114 | + mb.add(m2); |
| 115 | + mb.add(mc); |
| 116 | + |
| 117 | + f.setJMenuBar(mb); |
| 118 | + f.add(t); |
| 119 | + f.setSize(1000, 1000); |
| 120 | + f.setVisible(true); |
| 121 | + } |
| 122 | + |
| 123 | + // If a button is pressed |
| 124 | + public void actionPerformed(ActionEvent e) |
| 125 | + { |
| 126 | + String s = e.getActionCommand(); |
| 127 | + |
| 128 | + if (s.equals("cut")) { |
| 129 | + t.cut(); |
| 130 | + } |
| 131 | + else if (s.equals("copy")) { |
| 132 | + t.copy(); |
| 133 | + } |
| 134 | + else if (s.equals("paste")) { |
| 135 | + t.paste(); |
| 136 | + } |
| 137 | + else if (s.equals("Save")) { |
| 138 | + // Create an object of JFileChooser class |
| 139 | + JFileChooser j = new JFileChooser("f:"); |
| 140 | + |
| 141 | + // Invoke the showsSaveDialog function to show the save dialog |
| 142 | + int r = j.showSaveDialog(null); |
| 143 | + |
| 144 | + if (r == JFileChooser.APPROVE_OPTION) { |
| 145 | + |
| 146 | + // Set the label to the path of the selected directory |
| 147 | + File fi = new File(j.getSelectedFile().getAbsolutePath()); |
| 148 | + |
| 149 | + try { |
| 150 | + // Create a file writer |
| 151 | + FileWriter wr = new FileWriter(fi, false); |
| 152 | + |
| 153 | + // Create buffered writer to write |
| 154 | + BufferedWriter w = new BufferedWriter(wr); |
| 155 | + |
| 156 | + // Write |
| 157 | + w.write(t.getText()); |
| 158 | + |
| 159 | + w.flush(); |
| 160 | + w.close(); |
| 161 | + } |
| 162 | + catch (Exception evt) { |
| 163 | + JOptionPane.showMessageDialog(f, evt.getMessage()); |
| 164 | + } |
| 165 | + JOptionPane.showMessageDialog(f, "The file has been successfully saved!"); |
| 166 | + |
| 167 | + } |
| 168 | + // If the user cancelled the operation |
| 169 | + else |
| 170 | + JOptionPane.showMessageDialog(f, "You have cancelled the save!"); |
| 171 | + } |
| 172 | + else if (s.equals("Print")) { |
| 173 | + try { |
| 174 | + // print the file |
| 175 | + t.print(); |
| 176 | + } |
| 177 | + catch (Exception evt) { |
| 178 | + JOptionPane.showMessageDialog(f, evt.getMessage()); |
| 179 | + } |
| 180 | + } |
| 181 | + else if (s.equals("Open")) { |
| 182 | + // Create an object of JFileChooser class |
| 183 | + JFileChooser j = new JFileChooser("f:"); |
| 184 | + |
| 185 | + // Invoke the showsOpenDialog function to show the save dialog |
| 186 | + int r = j.showOpenDialog(null); |
| 187 | + |
| 188 | + // If the user selects a file |
| 189 | + if (r == JFileChooser.APPROVE_OPTION) { |
| 190 | + // Set the label to the path of the selected directory |
| 191 | + File fi = new File(j.getSelectedFile().getAbsolutePath()); |
| 192 | + |
| 193 | + try { |
| 194 | + // String |
| 195 | + String s1 = "", sl = ""; |
| 196 | + |
| 197 | + // File reader |
| 198 | + FileReader fr = new FileReader(fi); |
| 199 | + |
| 200 | + // Buffered reader |
| 201 | + BufferedReader br = new BufferedReader(fr); |
| 202 | + |
| 203 | + // Initilize sl |
| 204 | + sl = br.readLine(); |
| 205 | + |
| 206 | + // Take the input from the file |
| 207 | + while ((s1 = br.readLine()) != null) { |
| 208 | + sl = sl + "\n" + s1; |
| 209 | + } |
| 210 | + |
| 211 | + // Set the text |
| 212 | + t.setText(sl); |
| 213 | + } |
| 214 | + catch (Exception evt) { |
| 215 | + JOptionPane.showMessageDialog(f, evt.getMessage()); |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + // If the user cancelled the operation |
| 220 | + else |
| 221 | + JOptionPane.showMessageDialog(f, "You have cancelled the print!"); |
| 222 | + } |
| 223 | + else if (s.equals("New")) { |
| 224 | + t.setText(""); |
| 225 | + } |
| 226 | + |
| 227 | + else if (s.equals("Close")) { |
| 228 | + // f.setVisible(false); |
| 229 | + f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING)); |
| 230 | + |
| 231 | + } |
| 232 | + |
| 233 | + |
| 234 | + } |
| 235 | + |
| 236 | + // Main class |
| 237 | + public static void main(String args[]) |
| 238 | + { |
| 239 | + editor e = new editor(); |
| 240 | + } |
| 241 | +} |
0 commit comments