开发者

How to Clear variables from JTextFields without closing whole program?

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();
            }
       });
    }


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")


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜