Guest User

Untitled

a guest
Jul 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. package org.s1gma.tutorial;
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.*;
  7. import java.io.FileFilter;
  8. import java.util.Scanner;
  9.  
  10. /**
  11. * @author s1gma
  12. */
  13. public class Notepad extends JFrame implements ActionListener {// like it can use all the
  14. private JTextArea txt = new JTextArea(); // basically i had to make it local so that it could be used in more than 1 method yh i see thnx for explaining :d
  15.  
  16. private JMenuBar newMenubar() {
  17. JMenuBar menubar = new JMenuBar(); //Sets up the menubar
  18. String[] titles = {"File", "test"}; // leave lol ok
  19. String[][] elements = {{"New", "Open", "Save"}, {"LOL"}}; //allready there lol ok go :d k lol
  20. for(int i = 0; i < titles.length; i++) { // basically loops through the menu titles
  21. String title = titles[i]; // selects the titles from the loop
  22. String[] elems = elements[i];//basically finds the menuitems for the menu
  23. menubar.add(newMenu(title, elems)); // adds a new menu with the title and elements, u understand? lhl yes ;d
  24. //Okay now we add the menu to the frame and we will boot it up
  25.  
  26. }
  27. return menubar;//Returns the menubar ok
  28. }
  29.  
  30. /**
  31. *
  32. * @param title The title like "File"
  33. * @param elements The elements like "New", "Load", "Save"
  34. * @return returns the JMenu that you make o
  35. */
  36. private JMenu newMenu(String title, String[] elements) {
  37. JMenu menu = new JMenu(title); //Creates a new JMenu with the title ik
  38. for(String element : elements) { //u understand?yes :d
  39. JMenuItem menuitem = new JMenuItem(element);//already told you about this :Pok
  40. menu.add(menuitem); // uses the add method in the JMenu class for our menu to add them menuitems yh ok :d
  41. menuitem.addActionListener(this);//makes it so that the menuitems respond to the actionlistenerok
  42. }
  43. return menu;
  44. }
  45.  
  46. private Notepad() {
  47. setTitle("untitled - Notepad"); //Wanna add the title thing now? k we'll do it now
  48. try {
  49. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //basically gives it the system themeik
  50. } catch (ClassNotFoundException e) {
  51. e.printStackTrace();
  52. } catch (InstantiationException e) {
  53. e.printStackTrace();
  54. } catch (IllegalAccessException e) {
  55. e.printStackTrace();
  56. } catch (UnsupportedLookAndFeelException e) {
  57. e.printStackTrace();
  58. }
  59. setSize(800, 600); // straight forward lol
  60. setJMenuBar(newMenubar());
  61. JScrollPane scroller = new JScrollPane(txt);//it has the txt(JTextArea) in it to select what its the container fork
  62. add(scroller); // adds the scroller which has the text area in it.ok
  63. //Now this is where we have to make a menubar for files, i'll show you a nice way to do menus.ok
  64. //what else was i going to add?scrollbar oh yeah
  65. }
  66.  
  67. public static void main(String[] args) {
  68. new Notepad().setVisible(true);//loads and sets it visible ik :p
  69. }
  70.  
  71. //This shit here is for later to make the buttons and shit work. i know :d fk don't delete this class sent over msn soon :d
  72. public void actionPerformed(ActionEvent actionEvent) {
  73. String cmd = actionEvent.getActionCommand(); // basically retrieves what you've clickedok
  74. if(cmd.equals("Save")) { // If the button pressed has the text "Save" on it do something inside.
  75. //Forgot file chooser
  76. JFileChooser chooser = new JFileChooser(); // sets up the file choosing dialog. ok
  77. int option = chooser.showSaveDialog(this); // Shows the save dialog and is the option for what you've clicked
  78. if(option == JFileChooser.APPROVE_OPTION) { //if you've pressed the ok or save button or w/e do somethingok
  79. //also stop pressing o when i press control, it's frustrating l0lklol
  80. try {
  81. BufferedWriter buf = new BufferedWriter(new FileWriter(chooser.getSelectedFile().getAbsolutePath()));
  82. //^ Basically the bufferedwriter is something used for writing to a file along with filewriter
  83. // yeah and the chooser.getSelectedFile().getAbsolutePath() basically finds the place in the filechooserik
  84. //and writes to it.
  85. buf.write(txt.getText()); // basically this gets the text in the text area and writes it to the file
  86. setTitle(chooser.getSelectedFile().getName()); //this basically gets the file name in the chooser. rofl thisi s easy lol :d thnx, i told you it wasnt hard :Pyh
  87. buf.close(); // closes the stream for memory purposesk, now we can run it
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. } else if(cmd.equals("Open")) {
  93. JFileChooser chooser = new JFileChooser(); // filechooser object
  94. chooser.setFileFilter(new Filter());
  95. int option = chooser.showOpenDialog(this); // same as before but with open this time. ok w8
  96. if(option == JFileChooser.APPROVE_OPTION) {
  97. try {
  98. Scanner scanner = new Scanner(chooser.getSelectedFile()); // gets the selected file from chooser
  99. while(scanner.hasNext()) { // When the scanner still has stuff to read, do something
  100. String data = scanner.nextLine(); // Read lines inside the scanner
  101. txt.setText(data); // Puts the data it read from the file into the text area.k
  102. }
  103. setTitle(chooser.getSelectedFile().getName());
  104. //Problem is i havent used file filter in a while so i mite do something wrong. ok
  105. scanner.close(); // close the scanner for memory purposes.
  106. } catch (FileNotFoundException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
  111. }
  112.  
  113. class Filter extends javax.swing.filechooser.FileFilter implements FileFilter {
  114.  
  115. public boolean accept(File file) {
  116. return file.getName().endsWith(".txt") || file.getName().endsWith(".java");
  117. }
  118.  
  119. @Override
  120. public String getDescription() {
  121. return "Text File (.txt)";
  122. }
  123. }
  124. }
Add Comment
Please, Sign In to add comment