javanetbeansjframejform

how to save JFrame variables when the Frame is closed and another frame opened


I am trying to save a created profile on my J-Frame. My goal is to ask the user to enter details into a Text Field (such as name, age, gender) and then save their inputs when I close my frame and open the next frame (similar to a save and continue button). I then want to be able to go back to the profile Frame (in case editing is needed) and still have the text in the Text Field. How do I do this?


Solution

  • I solved my problem by creating a text file that is outside the src file, made a FileWriter which writes the text I needed to save to the text file. I then used a BufferedReader to access the data I needed and stored it in a variable for later use.

    //Create FileWriter to write text to the text file
    FileWritrer charName = new FileWriter("CharacterName.txt");
    charName.write("Text needing to be saved goes in here");
    charName.close();
    
    //Creates BufferedReader to get data from txt file and save the line read in a variable
    BufferedReader br = new BufferedReader(new FileReader("write.txt"));
    String lineRead= br.readLine();//storing read line in variable for later use
    System.out.println("The text in the first line goes here:\n"+ lineRead);
    br.close();
    

    This is the simple version, so hopefully it makes sense. If not feel free to correct me. Thanks and I hope it helps.