1

I have created a text file in which to store some variables which are taken from text fields. But in order to submit new variables to this text file, I need to close my program and reopen it. The dispose(); command closes the JFrame taking me to my main menu but upon opening the menu again and submitting different values, the values from the previous time have been resubmitted. Is there a simple way to amend this?

Here is my write to .txt code:

public class writeto {

static String data = AddProperty.inputdata;
BufferedWriter out;

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

            out.write(data);

            out.newLine();

            out.close();

        }catch(IOException e){

            System.out.println("you have an error" + e);
        }
    }
}

and where the method is called in my addproperty class

        submitproperty.addActionListener(new ActionListener()
        {

        public void actionPerformed(ActionEvent e)
            {
              housenumber1 = houseNumber.getText();
              streetname1 = streetName.getText();
              town1 = town.getText();
              postcode1 = postcode.getText();
              beds1 = beds.getText();
              price1 = price.getText();
              type1 = type.getText();

            inputdata = housenumber1 + " " + streetname1 + " " + town1 + " " + 

            postcode1 +" " + beds1 + " " + price1 + " " + type1;

             writeto write = new writeto();
             dispose();
            }
       });
    }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Darren Burgess
  • 4,200
  • 6
  • 27
  • 43
  • 1
    Basically a duplicate of http://stackoverflow.com/questions/6018678/clearing-jtextfields-to-write-multiple-data-to-txt-file/6018954#6018954 - the answer is the same (look at my answer). – Robin Green May 16 '11 at 15:42

2 Answers2

3

From your menu you should always create a new JFrame with new widgets (like text fields). A new text field has no content, if you show a text field again, it will still display it's previous content.


Additional remarks:

  • Please use standard naming conventions - not only when you show code to others. In your case: class names shall start with a capital letter, camel-case notation is preferred (writeto -> WriteTo)
  • The writeto class abuses the constructor. The code in your constructor does not create an writeto object but dumps some strings to a file. Put this kind of code to a method, not to a constructor.
  • The BufferedWriter will not be closed if an exception occurs. Look around at stackoverflow, a lot of questions/answers show the correct io-closeing pattern
  • disposing the jframe is a risk - the code is executed after pressing a button (correct?), inside a method on a button that is displayed on the frame (correct?). In that case the button may be disposed while a method on the button object is still executed.. Try setVisible(false) if you just want to hide the JFrame (like "close the dialog")
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
2

You would benefit greatly from using a database as opposed to a text file. Further your question displays a fundamental lack of knowledge of not only Swing, but basic CRUD (Create, Read, Update, Delete) functionality.

To answer your question you can clear your text field with textField1.setText("");

I would read up on using a database for storing data. It will make life much easier for you.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • For this particular project it has been requested in the criteria that writing to a text file is required as opposed to a database. Although i will be sure to try .setText("") as way of clearing the textfields. Thank you for pointing out my fundamental lack of knowledge :) – Darren Burgess May 16 '11 at 13:35
  • 1
    I apologize, I didn't meant to make you feel inadequate, we were all in your shoes at one point in our careers. I am sorry that you are constrained to using a text file, that really sucks. It can become difficult and expensive to fetch existing records. – maple_shaft May 16 '11 at 13:43