Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.FinalProject.view;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.time.LocalDate;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import com.FinalProject.view.ReportPanel;
- import javax.swing.*;
- import com.FinalProject.model.Worker;
- import com.FinalProject.model.Task;
- public class AddTaskPanel extends JFrame implements ActionListener{
- JFrame frame;
- String str = "";
- Task task1;
- JLabel lblDes, lblDuration,l1,lblwname;
- JTextField tfDes, tfDur,tfwname;
- JButton btnsubmit, clearbtn,btnback,btnadd,reportbtn;
- List <File> files = new ArrayList <File>();
- ReportPanel p = new ReportPanel() ;
- public AddTaskPanel()
- {
- setVisible(true);
- setSize(600, 300);
- setLayout(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setTitle("Add Task Form");
- l1 = new JLabel("Add Task Form - Type Details Below:");
- l1.setForeground(Color.blue);
- l1.setFont(new Font("Serif", Font.BOLD, 20));
- lblDes = new JLabel("Add Task:");
- lblDuration = new JLabel("Task Duration:");
- lblwname = new JLabel("Worker Name: ");
- p.setVisible(false);
- tfDes = new JTextField();
- tfDur = new JTextField();
- tfwname = new JTextField();
- btnsubmit = new JButton("Submit");
- clearbtn = new JButton("Clear");
- btnback = new JButton("Back");
- btnadd = new JButton("Add");
- reportbtn = new JButton("Report");
- btnsubmit.addActionListener(this);
- clearbtn.addActionListener(this);
- btnback.addActionListener(this);
- btnadd.addActionListener(this);
- reportbtn.addActionListener(this);
- lblDes.setBounds(100, 70, 400, 30);
- lblDuration.setBounds(100, 110, 200, 30);
- lblwname.setBounds(100, 150, 200, 30);
- l1.setBounds(100, 30, 400, 30);
- tfDes.setBounds(300, 70, 200, 30);
- tfDur.setBounds(300, 110, 200, 30);
- tfwname.setBounds(300, 150, 200, 30);
- btnsubmit.setBounds(230, 230, 80, 30);
- clearbtn.setBounds(360, 230, 80, 30);
- btnback.setBounds(20, 230, 80, 30);
- btnadd.setBounds(130, 230, 80, 30);
- reportbtn.setBounds(500, 230, 80, 30);
- add(lblDes);
- add(lblDuration);
- add(tfwname);
- add(l1);
- add(tfDur);
- add(tfDes);
- add(lblwname);
- add(btnsubmit);
- add(clearbtn);
- add(btnback);
- add(btnadd);
- add(reportbtn);
- }
- public static void main(String[] args) {
- new AddTaskPanel();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource().equals(btnsubmit)) {
- if(tfwname.getText().isEmpty() && tfDes.getText().isEmpty()&& tfDur.getText().isEmpty())
- JOptionPane.showMessageDialog(frame, "Please fill the form");
- else {
- File f = new File(tfwname.getText() + ".txt");
- files.add(f);
- try {
- PrintWriter pw = new PrintWriter(f);
- // if (f.length() == 0)
- pw.println("Worker Name: " +tfwname.getText());
- pw.println(str);
- pw.close();
- str="";
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- else if(e.getSource().equals(btnadd)) {
- if(tfwname.getText().isEmpty() && tfDes.getText().isEmpty()&& tfDur.getText().isEmpty())
- JOptionPane.showMessageDialog(frame, "Please fill the form");
- else {
- str += "Task: " + tfDes.getText() + " Duration: "+ tfDur.getText()+"\n";
- System.out.println(str);
- }
- }
- else if(e.getSource().equals(clearbtn))
- {
- tfDur.setText("");
- tfDes.setText("");
- tfwname.setText("");
- }
- else if(e.getSource().equals(btnback))
- {
- //MenuPanel mp = new MenuPanel();
- this.setVisible(false);
- //mp.setVisible(true);
- }
- else if(e.getSource().equals(reportbtn))
- {
- if(files.size()==0)
- JOptionPane.showMessageDialog(frame, "Please add tasks first");
- else {
- File f = new File("Report.txt");
- String data="";
- for (File file : files) {
- try {
- File myObj = new File(file.getName());
- Scanner myReader = new Scanner(file);
- while (myReader.hasNextLine()) {
- data += myReader.nextLine()+" \n";
- //System.out.println(data);
- }
- myReader.close();
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- data+="\n";
- }
- System.out.println(data);
- }
- try {
- PrintWriter pw = new PrintWriter(f);
- // if (f.length() == 0)
- pw.println("Report\n");
- pw.println(data);
- pw.close();
- p.ta.setText(data);
- this.setVisible(false);
- p.setVisible(true);
- data="";
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement