Search code examples
javadata-entry

Variable data entry to a notepad


I am currently working on a project which is basically a contact tracing form. The feature I am trying to implement is that when the "print" button is pressed, it enters the data to a notepad text. Although this project is just for fun, I am trying to complete it so that I can learn more about java.

Here is my code:


package com.company;

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class Main extends JFrame implements ActionListener {
    //Initialization of Form
    JPanel panel;
    JLabel Name_label;
    JLabel Address_label;
    JLabel Temp_label;
    JLabel Age_label;
    JLabel Contact_label;
    JLabel Date_label;
    JLabel msg_label;
    JLabel msg2_label;
    static JTextField address_text;
    static JTextField name_text;
    static JTextField age_text;
    static JTextField temp_text;
    static JTextField cn_text;
    static JTextField date_text;
    JButton submit, exit, print;


    Main() {
        //Name Label
        Name_label = new JLabel();
        Name_label.setText("Name: ");
        name_text = new JTextField();
        //Date Label
        Date_label = new JLabel();
        Date_label.setText("Date: ");
        date_text = new JTextField();
        //Address Label
        Address_label = new JLabel();
        Address_label.setText("Complete Address: ");
        address_text = new JTextField();
        //Contact Number
        Contact_label = new JLabel();
        Contact_label.setText("Contact Number");
        cn_text = new JTextField();
        //Age Label
        Age_label = new JLabel();
        Age_label.setText("Age:");
        age_text = new JTextField();
        //Temperature Label
        Temp_label = new JLabel();
        Temp_label.setText("Temperature: ");
        temp_text = new JTextField();
        //Submit
        submit = new JButton("SUBMIT");
        panel = new JPanel(new GridLayout(9, 3));
        exit = new JButton("EXIT");
        print = new JButton("Print");
        //Message
        msg_label = new JLabel();
        msg2_label = new JLabel();
        //Adding elements
        panel.add(Name_label);
        panel.add(name_text);
        panel.add(Date_label);
        panel.add(date_text);
        panel.add(Address_label);
        panel.add(address_text);
        panel.add(Contact_label);
        panel.add(cn_text);
        panel.add(Age_label);
        panel.add(age_text);
        panel.add(Temp_label);
        panel.add(temp_text);
        panel.add(msg_label);
        panel.add(msg2_label);
        panel.add(submit);
        panel.add(exit);
        panel.add(print);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //Listeners
        submit.addActionListener(this);
        add(panel, BorderLayout.CENTER);
        setTitle("Contact Tracing Form");
        setSize(1000, 530);
        setVisible(true);
        exit.addActionListener((event) -> System.exit(0));
        print.addActionListener(note());
    }

    private ActionListener note() {
        File file = new File("/users/agcao/contactform.txt");
        String name = name_text.getText();
        String date = date_text.getText();
        String address = address_text.getText();
        String age = age_text.getText();
        String cn = cn_text.getText();
        String temp = temp_text.getText();


        FileWriter fw = null;
        try {
            fw = new FileWriter(file.getAbsoluteFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert fw != null;
        BufferedWriter bw = new BufferedWriter(fw);
        try {
            bw.write(name);
            bw.write(date);
            bw.write(address);
            bw.write(age);
            bw.write(cn);
            bw.write(temp);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //step2: write it
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Done");
        return null;
    }

    public static void main(String[] args) throws IOException {
        new Main();
    }







    @Override
    public void actionPerformed(ActionEvent e) {
        //Initialization
        String name = name_text.getText();
        double tempe = Double.parseDouble(temp_text.getText());
        int Iage = Integer.parseInt(age_text.getText());
        //Process
        if (Iage < 15 && tempe >= 37.5)
            msg_label.setText("Sorry, " + name + ", You cannot enter this establishment due to your age and temperature!");
        else if (tempe >= 37.5)
            msg_label.setText("Sorry, " + name + ", You cannot enter this establishment due to your temperature!");
        else if (Iage < 15)
            msg_label.setText("Sorry," + name + "you cannot enter this establishment due to your age!");
        else
            msg_label.setText("Hello  " + name + ", You may enter this building. Thank you");

    }
}

    enter code here

sorry if it is too redundant or poorly made. I am still currently trying to become a better developer. Thanks


Solution

  • There's nothing wrong with your file-writing code, the problem is your return value. In the note() method, you specify that it returns an ActionListener, and you add that ActionListener to the print component. But if I look at your code, you don't return any ActionListener at all! You just return null;. The fix is simple: just return the ActionListener, not null. Below is your note() method, modified. For the sake of brevity, I have trimmed it to the first 3 lines.

    private ActionListener note() {
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                File file = new File("contactform.txt");
                String name = name_text.getText();
                String date = date_text.getText();
                ...
            }
        };
    }
    

    Now, the code above uses something called an anonymous class. I create a new subclass of ActionListener where I've overridden the actionPerformed() method, and then I return it. Because ActionListener only has one method, though, this code can be shortened even further by using a lambda expression. Here's an example using a lambda:

    private ActionListener note() {
        return (event) -> {
            File file = new File("contactform.txt");
            String name = name_text.getText();
            String date = date_text.getText();
            ...
        };
    }
    

    Another possibility, using a lambda, is to instead of returning an ActionListener, you insert the lambda when you add the ActionListener to the print component. This will work just as well, without modifying the note() method:

    print.addActionListener((event) -> note());