Search code examples
javauser-interfacetextfile-iojtextarea

open file into JTextArea on startup


I have a program and am trying to test to see if a file (info.txt) is created and if it is, open the contents into a JTextArea as the program starts up. How would i go about doing this? ive already figured out how to search and see if the file exists, just can not get it to open into the textArea on startup

import javax.swing.*; // need it
import java.awt.*;   //need it also
import java.awt.datatransfer.*;
import java.util.*;
import java.io.*;
import java.awt.event.*; //keeps track of events
import javax.swing.border.*; // not necessary / already imported ^^

import javax.swing.*; 
import java.awt.*;   
import java.awt.datatransfer.*;
import java.util.*;
import java.io.*;
import java.awt.event.*; 
import javax.swing.border.*;

public class finalA extends JFrame implements ActionListener{
 private final int WIDTH = 750;
 private final int HEIGHT = 400;
 static String theText;
 private String text;

 //creates components
 private JTextArea textArea;
 private JButton SaveB;
 private JScrollPane scroll;


 public finalA(){
 setTitle("Super fancy text editor");
 setSize(WIDTH,HEIGHT);

  Container pane = getContentPane();

  //Creates button
  SaveB = new JButton("Save & Exit");
  textArea = new JTextArea();
  scroll = new JScrollPane(textArea);
  scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

  //adds event handler for exit button

  SaveB.addActionListener(this);

  //sets pane to null
  pane.setLayout(null);

  //location of button and TA (750,400)
  textArea.setLocation(10,100);
  SaveB.setLocation(150,320);

  //set size of TA and Button
  textArea.setSize(730,200);
  SaveB.setSize(100,30);

  //add items to pane
  pane.add(textArea);
  pane.add(SaveB);

  scroll.setBounds(10,100,730,200);
  scroll.setVisible(true);

  textArea.setLineWrap(true);
  textArea.setWrapStyleWord(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  }//ends constructor

  public void openFile()throws Exception{
   File f = new File("info.txt");
   if(f.exists()){
    FileReader reader = null;
    try{
     reader = new FileReader("info.txt");
     textArea.read(reader, null);
    } 
    finally{
     if (reader != null){
        reader.close();
     }
    }
   }
  }//ends openFile

  public void actionPerformed(ActionEvent e){
   String text = textArea.getText();
   if(e.getActionCommand().equals("Save & Exit")){

    try{
     BufferedWriter reader = new BufferedWriter(new FileWriter(new File("info.txt"),true));
     reader.write(text);
     reader.newLine();
     reader.close();
    }catch(IOException E){
    System.out.println("the error is " + E);
    }
   }
   System.exit(0);



 } 
}// ends finalA class

Solution

  • So, based on your available code you should simply need to call openFile at the end of your constructor, after you've created the JTextArea

    public TestRead() {
        //...
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        try {
            openFile();
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }//ends constructor
    

    Don't use null layouts, pixel perfect layouts are an illusion in modern GUI design. You don't control factors like font metrics, DPI or rendering pipelines all of which can change the individual requirements of components.

    Swing has been designed to make use layout managers and much of the functionality that updates the interface is related to it. If you do away with the layout manager, be ready of a load of never ending work and frustration