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