From cb616c09b1a7d021e6bd45b1ed08fe55dc0b6d7c Mon Sep 17 00:00:00 2001 From: Vaidhyanathan S M Date: Wed, 20 Oct 2021 15:35:25 +0530 Subject: [PATCH 1/2] Added Text Editor Application --- .../BoraxCode/deployedLink.txt | 1 + .../Vaidhyanathan S M/BoraxCode/editor.java | 241 ++++++++++++++++++ .../Vaidhyanathan S M/BoraxCode/thumbnail.png | Bin 0 -> 11680 bytes 3 files changed, 242 insertions(+) create mode 100644 public/Projects/Vaidhyanathan S M/BoraxCode/deployedLink.txt create mode 100644 public/Projects/Vaidhyanathan S M/BoraxCode/editor.java create mode 100644 public/Projects/Vaidhyanathan S M/BoraxCode/thumbnail.png diff --git a/public/Projects/Vaidhyanathan S M/BoraxCode/deployedLink.txt b/public/Projects/Vaidhyanathan S M/BoraxCode/deployedLink.txt new file mode 100644 index 0000000..e136a6b --- /dev/null +++ b/public/Projects/Vaidhyanathan S M/BoraxCode/deployedLink.txt @@ -0,0 +1 @@ +https://github.com/smv1999/BoraxCode \ No newline at end of file diff --git a/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java b/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java new file mode 100644 index 0000000..2e5b0f6 --- /dev/null +++ b/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java @@ -0,0 +1,241 @@ +// Java Program to create a text editor using java +import java.awt.*; +import javax.swing.*; +import java.io.*; +import java.awt.event.*; +import javax.swing.plaf.metal.*; +import javax.swing.text.*; + +import javafx.stage.Modality; +class editor extends JFrame implements ActionListener { + // Text component + JTextArea t; + + // Frame + JFrame f; + + + + // Constructor + editor() + { + + + f = new JFrame("Borax Code"); + + ImageIcon img = new ImageIcon("text-editor-icon.png"); + f.setIconImage(img.getImage()); + + + + try { + // Set metl look and feel + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //javax.swing.plaf.windows.WindowsLookAndFeel + + } + catch (Exception e) { + } + + // Text component + + t = new JTextArea(); + + t.setFont(t.getFont().deriveFont(20f)); + + + + JScrollPane scroll = new JScrollPane (t); + scroll.setPreferredSize(t.getPreferredSize()); + scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ); + + + // Create a menubar + JMenuBar mb = new JMenuBar(); + + // Create a menu for menu + JMenu m1 = new JMenu("File"); + + m1.setPreferredSize(new Dimension(80, m1.getPreferredSize().height)); + + + + // Create menu items + JMenuItem mi1 = new JMenuItem("New"); + JMenuItem mi2 = new JMenuItem("Open"); + JMenuItem mi3 = new JMenuItem("Save"); + JMenuItem mi9 = new JMenuItem("Print"); + + + mb.add(Box.createVerticalGlue()); + + + // Add action listener + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + mi9.addActionListener(this); + + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + m1.add(mi9); + + // Create amenu for menu + JMenu m2 = new JMenu("Edit"); + m2.setPreferredSize(new Dimension(80, m2.getPreferredSize().height)); + + + // Create menu items + JMenuItem mi4 = new JMenuItem("cut"); + JMenuItem mi5 = new JMenuItem("copy"); + JMenuItem mi6 = new JMenuItem("paste"); + + + + + + // Add action listener + mi4.addActionListener(this); + mi5.addActionListener(this); + mi6.addActionListener(this); + + m2.add(mi4); + m2.add(mi5); + m2.add(mi6); + + JMenuItem mc = new JMenuItem("Close"); + + + + + mc.addActionListener(this); + + mb.add(m1); + mb.add(m2); + mb.add(mc); + + f.setJMenuBar(mb); + f.add(t); + f.setSize(1000, 1000); + f.setVisible(true); + } + + // If a button is pressed + public void actionPerformed(ActionEvent e) + { + String s = e.getActionCommand(); + + if (s.equals("cut")) { + t.cut(); + } + else if (s.equals("copy")) { + t.copy(); + } + else if (s.equals("paste")) { + t.paste(); + } + else if (s.equals("Save")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsSaveDialog function to show the save dialog + int r = j.showSaveDialog(null); + + if (r == JFileChooser.APPROVE_OPTION) { + + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // Create a file writer + FileWriter wr = new FileWriter(fi, false); + + // Create buffered writer to write + BufferedWriter w = new BufferedWriter(wr); + + // Write + w.write(t.getText()); + + w.flush(); + w.close(); + } + catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + JOptionPane.showMessageDialog(f, "The file has been successfully saved!"); + + } + // If the user cancelled the operation + else + JOptionPane.showMessageDialog(f, "You have cancelled the save!"); + } + else if (s.equals("Print")) { + try { + // print the file + t.print(); + } + catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } + else if (s.equals("Open")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsOpenDialog function to show the save dialog + int r = j.showOpenDialog(null); + + // If the user selects a file + if (r == JFileChooser.APPROVE_OPTION) { + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // String + String s1 = "", sl = ""; + + // File reader + FileReader fr = new FileReader(fi); + + // Buffered reader + BufferedReader br = new BufferedReader(fr); + + // Initilize sl + sl = br.readLine(); + + // Take the input from the file + while ((s1 = br.readLine()) != null) { + sl = sl + "\n" + s1; + } + + // Set the text + t.setText(sl); + } + catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + + } + // If the user cancelled the operation + else + JOptionPane.showMessageDialog(f, "You have cancelled the print!"); + } + else if (s.equals("New")) { + t.setText(""); + } + + else if (s.equals("Close")) { + // f.setVisible(false); + f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING)); + + } + + + } + + // Main class + public static void main(String args[]) + { + editor e = new editor(); + } +} diff --git a/public/Projects/Vaidhyanathan S M/BoraxCode/thumbnail.png b/public/Projects/Vaidhyanathan S M/BoraxCode/thumbnail.png new file mode 100644 index 0000000000000000000000000000000000000000..dcd5558be476820eb4d38de7c6f160795c387dd6 GIT binary patch literal 11680 zcmeHtc~n#9+HYEFIaY{6#xL(;d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?d00001 From 631a85b2e41043135b81bb7881ecd9cd32f66c9c Mon Sep 17 00:00:00 2001 From: Vaidhyanathan S M Date: Mon, 15 Nov 2021 21:51:00 +0530 Subject: [PATCH 2/2] Changed the formatting --- .../Vaidhyanathan S M/BoraxCode/editor.java | 346 ++++++++---------- 1 file changed, 157 insertions(+), 189 deletions(-) diff --git a/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java b/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java index 2e5b0f6..4f35e9a 100644 --- a/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java +++ b/public/Projects/Vaidhyanathan S M/BoraxCode/editor.java @@ -1,241 +1,209 @@ + // Java Program to create a text editor using java -import java.awt.*; -import javax.swing.*; -import java.io.*; -import java.awt.event.*; -import javax.swing.plaf.metal.*; +import java.awt.*; +import javax.swing.*; +import java.io.*; +import java.awt.event.*; +import javax.swing.plaf.metal.*; import javax.swing.text.*; -import javafx.stage.Modality; -class editor extends JFrame implements ActionListener { - // Text component - JTextArea t; +import javafx.stage.Modality; - // Frame - JFrame f; +class editor extends JFrame implements ActionListener { + // Text component + JTextArea t; - + // Frame + JFrame f; - // Constructor - editor() - { + // Constructor + editor() { - - f = new JFrame("Borax Code"); + f = new JFrame("Borax Code"); ImageIcon img = new ImageIcon("text-editor-icon.png"); f.setIconImage(img.getImage()); + try { + // Set metl look and feel + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // javax.swing.plaf.windows.WindowsLookAndFeel + } catch (Exception e) { + } - try { - // Set metl look and feel - UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //javax.swing.plaf.windows.WindowsLookAndFeel - - } - catch (Exception e) { - } + // Text component - // Text component - - t = new JTextArea(); + t = new JTextArea(); t.setFont(t.getFont().deriveFont(20f)); - - - JScrollPane scroll = new JScrollPane (t); + JScrollPane scroll = new JScrollPane(t); scroll.setPreferredSize(t.getPreferredSize()); - scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ); + scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + // Create a menubar + JMenuBar mb = new JMenuBar(); - // Create a menubar - JMenuBar mb = new JMenuBar(); - - // Create a menu for menu - JMenu m1 = new JMenu("File"); + // Create a menu for menu + JMenu m1 = new JMenu("File"); m1.setPreferredSize(new Dimension(80, m1.getPreferredSize().height)); - - - // Create menu items - JMenuItem mi1 = new JMenuItem("New"); - JMenuItem mi2 = new JMenuItem("Open"); - JMenuItem mi3 = new JMenuItem("Save"); - JMenuItem mi9 = new JMenuItem("Print"); - + // Create menu items + JMenuItem mi1 = new JMenuItem("New"); + JMenuItem mi2 = new JMenuItem("Open"); + JMenuItem mi3 = new JMenuItem("Save"); + JMenuItem mi9 = new JMenuItem("Print"); mb.add(Box.createVerticalGlue()); + // Add action listener + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + mi9.addActionListener(this); - // Add action listener - mi1.addActionListener(this); - mi2.addActionListener(this); - mi3.addActionListener(this); - mi9.addActionListener(this); - - m1.add(mi1); - m1.add(mi2); - m1.add(mi3); - m1.add(mi9); + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + m1.add(mi9); - // Create amenu for menu - JMenu m2 = new JMenu("Edit"); + // Create amenu for menu + JMenu m2 = new JMenu("Edit"); m2.setPreferredSize(new Dimension(80, m2.getPreferredSize().height)); + // Create menu items + JMenuItem mi4 = new JMenuItem("cut"); + JMenuItem mi5 = new JMenuItem("copy"); + JMenuItem mi6 = new JMenuItem("paste"); - // Create menu items - JMenuItem mi4 = new JMenuItem("cut"); - JMenuItem mi5 = new JMenuItem("copy"); - JMenuItem mi6 = new JMenuItem("paste"); - + // Add action listener + mi4.addActionListener(this); + mi5.addActionListener(this); + mi6.addActionListener(this); + m2.add(mi4); + m2.add(mi5); + m2.add(mi6); - + JMenuItem mc = new JMenuItem("Close"); - // Add action listener - mi4.addActionListener(this); - mi5.addActionListener(this); - mi6.addActionListener(this); + mc.addActionListener(this); - m2.add(mi4); - m2.add(mi5); - m2.add(mi6); + mb.add(m1); + mb.add(m2); + mb.add(mc); - JMenuItem mc = new JMenuItem("Close"); + f.setJMenuBar(mb); + f.add(t); + f.setSize(1000, 1000); + f.setVisible(true); + } - + // If a button is pressed + public void actionPerformed(ActionEvent e) { + String s = e.getActionCommand(); + if (s.equals("cut")) { + t.cut(); + } else if (s.equals("copy")) { + t.copy(); + } else if (s.equals("paste")) { + t.paste(); + } else if (s.equals("Save")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); - mc.addActionListener(this); + // Invoke the showsSaveDialog function to show the save dialog + int r = j.showSaveDialog(null); - mb.add(m1); - mb.add(m2); - mb.add(mc); + if (r == JFileChooser.APPROVE_OPTION) { - f.setJMenuBar(mb); - f.add(t); - f.setSize(1000, 1000); - f.setVisible(true); - } + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); - // If a button is pressed - public void actionPerformed(ActionEvent e) - { - String s = e.getActionCommand(); + try { + // Create a file writer + FileWriter wr = new FileWriter(fi, false); - if (s.equals("cut")) { - t.cut(); - } - else if (s.equals("copy")) { - t.copy(); - } - else if (s.equals("paste")) { - t.paste(); - } - else if (s.equals("Save")) { - // Create an object of JFileChooser class - JFileChooser j = new JFileChooser("f:"); + // Create buffered writer to write + BufferedWriter w = new BufferedWriter(wr); - // Invoke the showsSaveDialog function to show the save dialog - int r = j.showSaveDialog(null); + // Write + w.write(t.getText()); - if (r == JFileChooser.APPROVE_OPTION) { + w.flush(); + w.close(); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + JOptionPane.showMessageDialog(f, "The file has been successfully saved!"); - // Set the label to the path of the selected directory - File fi = new File(j.getSelectedFile().getAbsolutePath()); - - try { - // Create a file writer - FileWriter wr = new FileWriter(fi, false); - - // Create buffered writer to write - BufferedWriter w = new BufferedWriter(wr); - - // Write - w.write(t.getText()); - - w.flush(); - w.close(); - } - catch (Exception evt) { - JOptionPane.showMessageDialog(f, evt.getMessage()); - } - JOptionPane.showMessageDialog(f, "The file has been successfully saved!"); - - } - // If the user cancelled the operation + } + // If the user cancelled the operation else - JOptionPane.showMessageDialog(f, "You have cancelled the save!"); - } - else if (s.equals("Print")) { - try { - // print the file - t.print(); - } - catch (Exception evt) { - JOptionPane.showMessageDialog(f, evt.getMessage()); - } - } - else if (s.equals("Open")) { - // Create an object of JFileChooser class - JFileChooser j = new JFileChooser("f:"); - - // Invoke the showsOpenDialog function to show the save dialog - int r = j.showOpenDialog(null); - - // If the user selects a file - if (r == JFileChooser.APPROVE_OPTION) { - // Set the label to the path of the selected directory - File fi = new File(j.getSelectedFile().getAbsolutePath()); - - try { - // String - String s1 = "", sl = ""; - - // File reader - FileReader fr = new FileReader(fi); - - // Buffered reader - BufferedReader br = new BufferedReader(fr); - - // Initilize sl - sl = br.readLine(); - - // Take the input from the file - while ((s1 = br.readLine()) != null) { - sl = sl + "\n" + s1; - } - - // Set the text - t.setText(sl); - } - catch (Exception evt) { - JOptionPane.showMessageDialog(f, evt.getMessage()); - } - - } - // If the user cancelled the operation + JOptionPane.showMessageDialog(f, "You have cancelled the save!"); + } else if (s.equals("Print")) { + try { + // print the file + t.print(); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } else if (s.equals("Open")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsOpenDialog function to show the save dialog + int r = j.showOpenDialog(null); + + // If the user selects a file + if (r == JFileChooser.APPROVE_OPTION) { + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // String + String s1 = "", sl = ""; + + // File reader + FileReader fr = new FileReader(fi); + + // Buffered reader + BufferedReader br = new BufferedReader(fr); + + // Initilize sl + sl = br.readLine(); + + // Take the input from the file + while ((s1 = br.readLine()) != null) { + sl = sl + "\n" + s1; + } + + // Set the text + t.setText(sl); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + + } + // If the user cancelled the operation else - JOptionPane.showMessageDialog(f, "You have cancelled the print!"); - } - else if (s.equals("New")) { - t.setText(""); - } - - else if (s.equals("Close")) { - // f.setVisible(false); + JOptionPane.showMessageDialog(f, "You have cancelled the print!"); + } else if (s.equals("New")) { + t.setText(""); + } + + else if (s.equals("Close")) { + // f.setVisible(false); f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING)); - } + } - - } + } - // Main class - public static void main(String args[]) - { - editor e = new editor(); - } -} + // Main class + public static void main(String args[]) { + editor e = new editor(); + } +}