Clearing JTextfields to write multiple data to txt file

北城余情 提交于 2020-02-02 14:06:42

问题


excuse the probably simple question (and the terrible layout methods). The code I have successfully writes inputted data to a txt file and on clicking "submit" closes the input window, leaving the "menu" open, with options to add a user (this code) or search properties (unrelated). I can enter one set of details to the txt file with no problem, but when reopening the AddUser window, no matter what is typed in to the boxes the same data is inputted into the file as the previous time, unless the program is closed. I think it has something to do with clearing some variable before reopening the window (as tried towards the bottom) however i haven't had any luck.. How do I go about it? Thanks

AddUser.java

package assignment; 
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.lang.*;

public class AddUser extends JFrame {

    //Declare the array values

    private String[] Name;
    private String[] Username;
    private String[] Password;
    private String[] StaffID;

    public String inputStaff;
    public String inputUser;
    public String inputPass;
    public String inputID;

    static public String inputData;


    //Declare Text Fields
    public JTextField Field1;
    public JTextField Field2;
    public JTextField Field3;
    public JTextField Field4;
    public JTextField Field5;

    //Declare Labels

    private JLabel Label;
    private JLabel Label1;
    private JLabel Label2;
    private JLabel Label3;
    private JLabel Label4;
    private JLabel Label5;
    private JLabel Space1;
    private JLabel Space2;

public AddUser() {

    super("Add New Agent");     //Window Title
    setLayout(new FlowLayout(FlowLayout.LEFT));    //Set Layout Type as FlowLayout

    Label = new JLabel("Enter the Member of Staff's Details");
    Label1 = new JLabel("Staff Name");    //Label Values
    Label2 = new JLabel("Username");
    Label3 = new JLabel("Password");
    Label4 = new JLabel("Confirm Password");
    Label5 = new JLabel("Staff ID");
    Space1 = new JLabel("    ");
    Space2 = new JLabel("                                       ");

    Field1 = new JTextField (10);   //Create the Text Fields and Option Blocks & Arguments
    Field2 = new JTextField (10);
    Field3 = new JTextField (10);
    Field4 = new JTextField (10);
    Field5 = new JTextField (4);






    //Add the labels, textfields and option blocks to the JFrame

    add (Label); add(Space1); add (Label1); add (Field1); add (Label2); add (Field2); add (Label3); add (Field3); add (Label4);
    add (Field4); add (Label5); add (Field5); add (Space2);





    JButton button1 = new JButton("Submit");    //Add "Search" button to JFrame
    add (button1);

    onClick handler = new onClick();
    button1.addActionListener(handler);

    }

    private class onClick implements ActionListener{
        public void actionPerformed(ActionEvent event){

//Action to be performed

//Attempt to clear the fields

           inputStaff = ("");
           inputUser = ("");
           inputPass = ("");
           inputID = ("");




           inputStaff = Field1.getText();
           inputUser = Field2.getText();
           inputPass = Field3.getText();
           inputID = Field5.getText();

           inputData = inputStaff + " " + inputUser + " " + inputPass + " " + inputID;


           WriteFile Write = new WriteFile(); //Create instance of write-to-file

           setVisible(false);
            //Close the window on clicking submit



               }

            }


           }

The write to file code (WriteFile.java) is as follows;

package assignment;

import java.io.*;

public class WriteFile{
static String data = AddUser.inputData;
BufferedWriter out;

public WriteFile(){
    try {
        out = new BufferedWriter(new FileWriter("AddUser.txt", true));

        out.write(data);

        out.newLine();

        out.close();
    }
    catch(IOException e)
    {
        System.out.println("There was a problem:" + e);

    }
}


}

回答1:


This way to achieve that lacks in a few manners, please consider the following:

public static void WriteFile(String data){
    try {
        out = new BufferedWriter(new FileWriter("AddUser.txt", true));
        out.write(data);
        out.newLine();
        out.close();
    }
    catch(IOException e)
    {
        System.out.println("There was a problem:" + e);
    }
}

And call it like that:

WriteFile.WriteFile(inputData);

I would also change the name of the method, but I tried to keep it as close as possible to the original code.

Don't access fields of a class in this way SomeClass.someField, and try to avoid static members when they are not needed.




回答2:


The line

static String data = AddUser.inputData;

is only run once, when the class is loaded. That is the case with all static variables. (You appear to be thinking along the lines of "dataflow programming" or spreadsheets, but Java doesn't work like that. Or you might be thinking that String objects are updateable, but they're not - they're immutable.)

This is a terrible way to implement data passing between classes, and as you can see, it doesn't work. It wouldn't even work once if for some reason that class happened to be loaded earlier than it was.



来源:https://stackoverflow.com/questions/6018678/clearing-jtextfields-to-write-multiple-data-to-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!