Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @(#)choresManager.java
- *
- *
- * @Karan
- * @version 1.00 2014/5/15
- */
- import javax.swing.*;//
- import java.awt.*; // background and buttons
- import java.awt.event.*; //
- import java.util.ArrayList; // array lists
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- import java.io.FileNotFoundException;
- public class choresManager extends JFrame implements ActionListener {
- static ArrayList<JTextArea> texts = new ArrayList<JTextArea>() ;
- ArrayList<JButton> buttons = new ArrayList<JButton>() ;
- JButton dadADD, dadUpdate;
- JFrame main, dadF;
- JButton dadLBL,momLBL,sonLBL,daughterLBL; // Labels for the household members
- JLabel titleLBL;
- public choresManager()
- {
- main = new JFrame();
- main.setLayout(null);
- main.setSize(1368,700);
- main.setVisible(true);
- main.setTitle("Chores Manager");
- dadF = new JFrame();
- dadF.setLayout(null);
- dadF.setSize(1368,700);
- //f2.setVisible(true);
- dadF.setTitle("Dad Chores");
- // create the labels
- titleLBL = new JLabel("Chores Manager");
- titleLBL.setBounds(500,50,500,100);
- Font f = new Font("Arial",Font.BOLD,36);
- titleLBL.setFont(f);
- main.add(titleLBL);
- dadLBL = new JButton (new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\Father.png"));
- dadLBL.setBounds(200,240,102,167);
- dadLBL.addActionListener(this);
- main.add(dadLBL);
- momLBL = new JButton(new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\Mother.png"));
- momLBL.setBounds(400,240,76,165);
- main.add(momLBL);
- sonLBL = new JButton (new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\son.png"));
- sonLBL.setBounds(600,240,67,167);
- main.add(sonLBL);
- daughterLBL = new JButton (new ImageIcon("C:\\Users\\Karan\\Google Drive\\Back up\\IB2\\Computer Science\\IA\\Code\\Daughter.jpg"));
- daughterLBL.setBounds(800,240,165,165);
- main.add(daughterLBL);
- main.repaint();
- }
- public static void main (String[]args)
- {
- new choresManager();
- }//end main
- public void actionPerformed (ActionEvent buttonName)
- {
- if (buttonName.getSource()==dadLBL)
- {
- dadF.setVisible(true);
- dadADD = new JButton("Add");
- dadADD.setBounds(1200,50,75,50);
- dadADD.addActionListener(this);
- dadF.add(dadADD);
- dadUpdate = new JButton("Update");
- dadUpdate.setBounds(1200,150,75,50);
- dadUpdate.addActionListener(this);
- dadF.add(dadUpdate);
- main.setVisible(false);
- //addTask();
- //addButton();
- dadSavedChores();
- dadF.repaint();
- //dadUpdateFunction(texts);
- }
- if (buttonName.getSource()==dadADD)
- {
- addTask();
- addButton();
- }
- if(buttonName.getSource()==dadUpdate)
- {
- dadUpdateFunction(texts);
- }
- }
- public ArrayList<JTextArea> addTask()
- {
- texts.add(new JTextArea("Please enter new Chore here."));
- int i = 1;
- for(JTextArea j : texts)
- {
- j.setBounds(200,(50+(55*i)),700,50);
- i++;
- dadF.add(j);
- }
- dadF.repaint();
- return texts;
- }
- public ArrayList<JButton> addButton()
- {
- buttons.add(new JButton("done"));
- int i = 1;
- for(JButton j : buttons)
- {
- j.setBounds(1000,(50+(50*i)),75,50);
- i++;
- dadF.add(j);
- }
- dadF.repaint();
- return buttons;
- }
- public ArrayList<JTextArea> dadSavedChores()
- {
- int i = 1;
- for(JTextArea j : texts)
- {
- j.setBounds(200,(50+(50*i)),700,50);
- i++;
- dadF.add(j);
- }
- //READER
- Scanner scanner = new Scanner(System.in); // create scanner
- BufferedReader br = null; // create buffered reader, accepts null intially
- String line; // created variable line
- try{
- br = new BufferedReader( new FileReader("C:\\Users\\Karan\\Documents\\dadTXT.txt")); //try to read file
- }
- catch(FileNotFoundException fnfex) // if file is not found
- {
- System.out.println(fnfex.getMessage() + "This file was not found");
- }
- //Here we read the lines
- try{ // handles reading lines
- while((line = br.readLine()) != null) // reads the line as long as there is a line
- {
- if(br.readLine()!= "")
- texts.add(new JTextArea(line));
- }
- }
- catch (IOException ioex) //if there is anything wrong
- {
- System.out.println(ioex.getMessage() + "error reading file");
- }
- return texts;
- }
- public void dadUpdateFunction(ArrayList<JTextArea> texts)
- {
- try{
- //Specify the file name and path here
- File file =new File("C:\\Users\\Karan\\Documents\\dadTXT.txt");
- /* This logic is to create the file if the
- * file is not already present
- */
- if(!file.exists()){
- file.createNewFile();
- }
- //Here true is to append the content to file
- FileWriter fw = new FileWriter(file,true);
- //BufferedWriter writer give better performance
- BufferedWriter bw = new BufferedWriter(fw);
- String content = "Karan";
- int i= 0;
- for(i= 0; i<texts.size(); i++)
- {
- content = (texts.get(i).getText() );
- if(i!=0)
- bw.newLine();
- if(i>0)
- texts.get(i-1).setEditable(false);
- }
- bw.write(content + "\n");
- //Closing BufferedWriter Stream
- bw.close();
- System.out.println("Data successfully appended at the end of file");
- }catch(IOException ioe){
- System.out.println("Exception occurred:");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment