-1

I have no clue how to read a line from a text file and put it in a JTextField. I have this so far to create a text file and be able to change the text inside it by changing my JTextFields. An example of this:

  public void outputFile() throws FileNotFoundException{

    PrintStream output = new PrintStream(new FileOutputStream("test"));
    output.println(jtextfield1.getText());

  }

I run this method when I click on a button. It works and the text file is indeed being changed. The problem is that I want to read this newly changed text to my JTextField, and the reason for this is that I use removeAll() method when changing between the panels for this program, thus my JTextField will be set as default (empty) when going back to this panel again.

For instance if I have written "abc" in my JTextField and clicked "OK" button, then when going back to my panel with my JTextField - my JTextField has to stay at "abc" and not get resetted (thus being empty), and the solution for this is to read from a file (but a line only, as there are many JTextFields) in to JTextField.

user3161041
  • 13
  • 3
  • 8
  • What do you mean by going back to panel? Are you using JOptionPane to read the value to append to file? – Syam S May 12 '14 at 12:00
  • 1
    I am using BorderLayout. I do not know whether I told you guys wrong information. My CENTER panel is changing, when I press on buttons from NORTH panel. So my JTextFields will be removed and added from and to the CENTER panel, when I change between the "menus" – user3161041 May 12 '14 at 12:27

3 Answers3

0

You can use Scanner class to read in your file line by line and you can have something like this:

Scanner s = new Scanner(new File("test"));

if(s.hasNextLine()){
    jtextfield1.setText(s.nextLine());
}

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

If you want the text to not get reset, then you need to keep a "structure" which keeps the values you have read from a Panel. So, when you set text for a JtextField, you need to give this value to that "Structure" or "global variable" so that it keeps the value.

You can do following:

  1. Define a structure which holds the values.
  2. Set those values when you are pressing next on a panel
  3. When coming back to panel, fill the values from structure by default and then give user option to edit the values or read from files.
  4. When finally writing the values, read them from structure itself.
Abhishek
  • 375
  • 1
  • 4
  • 12
  • Thanks for reply. I do not know whether it will be possible for you to show me an example so that I may understand it properly? – user3161041 May 12 '14 at 12:25
  • I can give you an example but it is better that you try this on your own. Just think of a solution and provide that here. This way I will be able to help you out more since an effort should be there from your end. – Abhishek May 13 '14 at 08:07
  • I could use a global variable as suggested by someone, and that worked. But then I realised that I had to use text files, as I have to get different values according to which of my indexes is chosen from my JComboBox. I have no clue how to insert in to JTextField from file. Creating the file - I have tried that by using PrintStream and that works well. My only problem so far is this getting the text from the file to the JTextField. – user3161041 May 13 '14 at 08:19
0

Don't use removeAll only remove what you need to remove.

Or you could make a temp variable that stores what is in the box right before clearing all then right after set it to that temp

String temp = textfield1.getText(); //Creates temporary string to hold the data in the text field
removeAll(); // your remove all call
textfield1.setText(temp); // sets the text to the temp variable you created earlier

This may not be exact since i do not know the st text method call off the top of my head but it should be something similar to set the text of a field

CrimOudin
  • 5
  • 3
  • I have to use removeAll, as the NORTH buttons have to show very differently things in CENTER panel. How to use that temp variable? Would it be possible for you to show an example? I am a bit new in this sorry. – user3161041 May 12 '14 at 12:32
  • String temp = textfield1.getText() removeAll() textfield1.setText(temp) – CrimOudin May 12 '14 at 12:35
  • What is the
    removeAll();? Is it the command executed from the removeAll() method from my panel? If it is center, then: center.removeAll(); ??
    – user3161041 May 12 '14 at 12:39
  • Sorry i was having formatting issues with this comment thing. Ill add the example to my answer ina edit – CrimOudin May 12 '14 at 12:44