Karankhosla

choresManager

Mar 11th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. /**
  2. * @(#)choresManager.java
  3. *
  4. *
  5. * @Karan
  6. * @version 1.00 2014/5/15
  7. */
  8.  
  9. import javax.swing.*;//
  10. import java.awt.*; // background and buttons
  11. import java.awt.event.*; //
  12. import java.util.ArrayList; // array lists
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileWriter;
  17. import java.io.BufferedWriter;
  18. import java.io.FileReader;
  19. import java.io.IOException;
  20. import java.util.Scanner;
  21. import java.io.FileNotFoundException;
  22.  
  23.  
  24. public class choresManager extends JFrame implements ActionListener {
  25. static ArrayList<JTextArea> texts = new ArrayList<JTextArea>() ;
  26. ArrayList<JButton> buttons = new ArrayList<JButton>() ;
  27. JButton dadADD, dadUpdate;
  28. JFrame main, dadF;
  29. JButton dadLBL,momLBL,sonLBL,daughterLBL; // Labels for the household members
  30. JLabel titleLBL;
  31.  
  32.  
  33.  
  34. public choresManager()
  35. {
  36. main = new JFrame();
  37. main.setLayout(null);
  38. main.setSize(1368,700);
  39. main.setVisible(true);
  40. main.setTitle("Chores Manager");
  41.  
  42. dadF = new JFrame();
  43. dadF.setLayout(null);
  44. dadF.setSize(1368,700);
  45. //f2.setVisible(true);
  46. dadF.setTitle("Dad Chores");
  47.  
  48.  
  49. // create the labels
  50. titleLBL = new JLabel("Chores Manager");
  51. titleLBL.setBounds(500,50,500,100);
  52. Font f = new Font("Arial",Font.BOLD,36);
  53. titleLBL.setFont(f);
  54. main.add(titleLBL);
  55.  
  56. dadLBL = new JButton (new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\Father.png"));
  57. dadLBL.setBounds(200,240,102,167);
  58. dadLBL.addActionListener(this);
  59. main.add(dadLBL);
  60.  
  61. momLBL = new JButton(new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\Mother.png"));
  62. momLBL.setBounds(400,240,76,165);
  63. main.add(momLBL);
  64.  
  65.  
  66. sonLBL = new JButton (new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\son.png"));
  67. sonLBL.setBounds(600,240,67,167);
  68. main.add(sonLBL);
  69.  
  70.  
  71. daughterLBL = new JButton (new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\Daughter.jpg"));
  72. daughterLBL.setBounds(800,240,165,165);
  73. main.add(daughterLBL);
  74.  
  75. main.repaint();
  76.  
  77.  
  78. }
  79.  
  80. public static void main (String[]args)
  81. {
  82. new choresManager();
  83.  
  84. }//end main
  85.  
  86.  
  87. public void actionPerformed (ActionEvent buttonName)
  88. {
  89. if (buttonName.getSource()==dadLBL)
  90. {
  91. dadF.setVisible(true);
  92.  
  93. dadADD = new JButton("Add");
  94. dadADD.setBounds(1200,50,75,50);
  95. dadADD.addActionListener(this);
  96. dadF.add(dadADD);
  97.  
  98. dadUpdate = new JButton("Update");
  99. dadUpdate.setBounds(1200,150,75,50);
  100. dadUpdate.addActionListener(this);
  101. dadF.add(dadUpdate);
  102.  
  103.  
  104. main.setVisible(false);
  105. //addTask();
  106. //addButton();
  107. dadSavedChores();
  108. dadF.repaint();
  109. //dadUpdateFunction(texts);
  110.  
  111. }
  112.  
  113. if (buttonName.getSource()==dadADD)
  114. {
  115. addTask();
  116. addButton();
  117.  
  118.  
  119. }
  120.  
  121. if(buttonName.getSource()==dadUpdate)
  122. {
  123. dadUpdateFunction(texts);
  124.  
  125. }
  126. }
  127.  
  128. public ArrayList<JTextArea> addTask()
  129. {
  130.  
  131.  
  132. texts.add(new JTextArea("Please enter new Chore here."));
  133.  
  134. int i = 1;
  135. for(JTextArea j : texts)
  136. {
  137. j.setBounds(200,(50+(55*i)),700,50);
  138.  
  139. i++;
  140. dadF.add(j);
  141. }
  142.  
  143.  
  144.  
  145. dadF.repaint();
  146. return texts;
  147.  
  148. }
  149.  
  150. public ArrayList<JButton> addButton()
  151. {
  152.  
  153.  
  154. buttons.add(new JButton("done"));
  155.  
  156. int i = 1;
  157. for(JButton j : buttons)
  158. {
  159. j.setBounds(1000,(50+(50*i)),75,50);
  160. i++;
  161. dadF.add(j);
  162. }
  163.  
  164.  
  165.  
  166. dadF.repaint();
  167. return buttons;
  168.  
  169. }
  170.  
  171. public ArrayList<JTextArea> dadSavedChores()
  172. {
  173.  
  174.  
  175.  
  176. int i = 1;
  177. for(JTextArea j : texts)
  178. {
  179. j.setBounds(200,(50+(50*i)),700,50);
  180. i++;
  181. dadF.add(j);
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. //READER
  190.  
  191. Scanner scanner = new Scanner(System.in); // create scanner
  192. BufferedReader br = null; // create buffered reader, accepts null intially
  193. String line; // created variable line
  194.  
  195.  
  196. try{
  197. br = new BufferedReader( new FileReader("C:\\Users\\Karan\\Documents\\dadTXT.txt")); //try to read file
  198.  
  199. }
  200. catch(FileNotFoundException fnfex) // if file is not found
  201. {
  202. System.out.println(fnfex.getMessage() + "This file was not found");
  203. }
  204. //Here we read the lines
  205. try{ // handles reading lines
  206.  
  207.  
  208. while((line = br.readLine()) != null) // reads the line as long as there is a line
  209. {
  210.  
  211. if(br.readLine()!= "")
  212. texts.add(new JTextArea(line));
  213. }
  214. }
  215. catch (IOException ioex) //if there is anything wrong
  216. {
  217. System.out.println(ioex.getMessage() + "error reading file");
  218. }
  219.  
  220.  
  221. return texts;
  222.  
  223.  
  224. }
  225.  
  226.  
  227.  
  228. public void dadUpdateFunction(ArrayList<JTextArea> texts)
  229. {
  230. try{
  231.  
  232.  
  233.  
  234. //Specify the file name and path here
  235. File file =new File("C:\\Users\\Karan\\Documents\\dadTXT.txt");
  236.  
  237. /* This logic is to create the file if the
  238. * file is not already present
  239. */
  240. if(!file.exists()){
  241. file.createNewFile();
  242. }
  243.  
  244. //Here true is to append the content to file
  245. FileWriter fw = new FileWriter(file,true);
  246. //BufferedWriter writer give better performance
  247. BufferedWriter bw = new BufferedWriter(fw);
  248. String content = "Karan";
  249. int i= 0;
  250.  
  251. for(i= 0; i<texts.size(); i++)
  252. {
  253.  
  254. content = (texts.get(i).getText() );
  255.  
  256.  
  257.  
  258. if(i!=0)
  259. bw.newLine();
  260.  
  261.  
  262. if(i>0)
  263. texts.get(i-1).setEditable(false);
  264.  
  265.  
  266. }
  267. bw.write(content + "\n");
  268. //Closing BufferedWriter Stream
  269. bw.close();
  270.  
  271. System.out.println("Data successfully appended at the end of file");
  272.  
  273. }catch(IOException ioe){
  274. System.out.println("Exception occurred:");
  275. }
  276.  
  277.  
  278.  
  279.  
  280. }
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment