I have a user password application that writes onto a file - you enter the information in the textfields, click a button and it should write to the next line of the file created.
There are two problems:
- The file only writes one line of data
- It writes only what you entered last. I have tried validate()andrepaint()but no difference.
public class Main {
public JFrame frame;
public JTextField textField;
public JTextField textField_1;
public JTextField textField_2;
public JTextField textField_3;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main window = new Main();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public Main() {
    initialize();
}
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 588, 156);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    JButton btnNewButton = new JButton("Add Record");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            openFile();
            printFormat();
            addRecords();
            closeFile();
        }
    });
    btnNewButton.setBounds(237, 83, 91, 23);
    frame.getContentPane().add(btnNewButton);
    textField = new JTextField();
    textField.setColumns(10);
    textField.setBounds(10, 44, 128, 20);
    frame.getContentPane().add(textField);
    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(148, 44, 128, 20);
    frame.getContentPane().add(textField_1);
    textField_2 = new JTextField();
    textField_2.setFont(UIManager.getFont("PasswordField.font"));
    textField_2.setColumns(10);
    textField_2.setBounds(286, 44, 128, 20);
    frame.getContentPane().add(textField_2);
    textField_3 = new JTextField();
    textField_3.setColumns(10);
    textField_3.setBounds(424, 44, 128, 20);
    frame.getContentPane().add(textField_3);
    JLabel lblNewLabel = new JLabel("Username");
    lblNewLabel.setBounds(10, 19, 128, 14);
    frame.getContentPane().add(lblNewLabel);
    JLabel lblPassword = new JLabel("Email");
    lblPassword.setBounds(148, 19, 128, 14);
    frame.getContentPane().add(lblPassword);
    JLabel lblMojangPassword = new JLabel("Mojang Password");
    lblMojangPassword.setBounds(286, 19, 128, 14);
    frame.getContentPane().add(lblMojangPassword);
    JLabel lblEmailPassword = new JLabel("Email Password");
    lblEmailPassword.setBounds(424, 19, 128, 14);
    frame.getContentPane().add(lblEmailPassword);
}
private Formatter x;
public void openFile(){
    try{
        x = new Formatter("okey.fil");
    }
    catch(Exception e){
        System.out.println("error get it right");
    }
}
public void printFormat(){
    x.format("%-25s %-25s %-25s %-25s \n", "Username", "Email", "Mojang Password", "Email Password");
}
public String getUsername(){
    String username = textField.getText();
    return username;
}
public void addRecords(){
        x.format("%-25s %-25s %-25s %-25s \n", getUsername(),textField_1.getText(),textField_2.getText(),textField_3.getText());
        x.format("\n"); 
        resetFields();
    }
public void resetFields(){
    textField.setText("");
    textField_1.setText("");
    textField_2.setText("");
    textField_3.setText("");
    textField.validate();
    textField_1.validate();
    textField_2.validate();
    textField_3.validate();
}
public void closeFile(){
    x.close();
}
}
 
     
     
     
    