Search code examples
javaswinguser-interfacebufferedreaderfilereader

Reading mutiple lines from a file in a Java GUI


I am a student and I am having troubles having my program read all the lines in a text file when I enter it into the program that has my GUI, it will only display the last line. I got it working on console and it works great and displays every line. Can anyone help?

here is the code that works in Console

    String parent = "D:\\Online Class\\Java Programs\\Final Project\\Tests\\";
    Scanner fileInput = new Scanner(System.in);

    System.out.print("Enter Name of File: ");
    String name= fileInput.nextLine();
    String extension = ".txt";
    String fileName = (parent+name+extension);

    //reads file and displays it
    String line = null;

    try{
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) !=null)
        {
            System.out.println(line);
        }
        bufferedReader.close();
    }

    catch(FileNotFoundException ex)
    {
        System.out.println("Unable to open file '" + fileName + "'");
    }
    catch(IOException ex)
    {
        System.out.println("Error Reading file '"+fileName + "'");
    }
}

and here is the code that does not work when I enter it to the program with the GUI

//search for file (must be in same folder as program)
    String parent = "D:\\Online Class\\Java Programs\\Final Project\\Tests\\";
    String fileInput = taskNameTextField.getText();
    String name = fileInput;
    String extension = ".txt";

    String fileName = (parent+name+extension);

    //reads file and displays it
    String line = null;

    try{
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) !=null)
        {
            System.out.println(line);
        }
        bufferedReader.close();
    }

    catch(FileNotFoundException ex)
    {
        System.out.println("Unable to open file '" + fileName + "'");
    }
    catch(IOException ex)
    {
        System.out.println("Error Reading file '"+fileName + "'");
    }
}

Here is the full code for the GUI that is acting up:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.Scanner;

public class readGUItest implements ActionListener
{

    JFrame window = new JFrame("Task");
    JTextField taskNameTextField = new JTextField();
    JTextArea DescriptTextArea = new JTextArea();
    JLabel taskNameLabel = new JLabel("Task Name");
    JLabel descriptLable = new JLabel("Description");
    JButton save = new JButton("save");



    public static void main(String[] args)throws IOException
    {
        readGUItest tGt = new readGUItest();
    }

    public readGUItest()
    {

        window.add(taskNameLabel);
        window.add(taskNameTextField);
        window.add(descriptLable);
        window.add(DescriptTextArea);
        window.add(save);
        save.addActionListener(this);

        window.setSize(new Dimension(200,300));
        window.setLayout(new GridLayout(3,3));
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }



    public void actionPerformed(ActionEvent event)
    {
        //search for file (must be in same folder as program)
        String parent = "D:\\Online Class\\Java Programs\\Final Project\\Tests\\";
        String fileInput = taskNameTextField.getText();
        String name = fileInput;
        String extension = ".txt";

        String fileName = (parent+name+extension);

        String line = null;

        try{
            FileReader fileReader = new FileReader(fileName);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) !=null)
            {
                DescriptTextArea.setText(line);
            }
            bufferedReader.close();
        }

        catch(FileNotFoundException ex)
        {
            DescriptTextArea.setText("Unable to open file '" + fileName + "'");
        }
        catch(IOException ex)
        {
            DescriptTextArea.setText("Error Reading file '"+fileName + "'");
        }
    }

}

Solution

  • You are using setText on the DescriptTextArea, which replaces the previous contents of the widget.

    You could rather use the document of the JTextField instead like this:

    DescriptTextArea.getDocument().insertString(
        DescriptTextArea.getDocument().getEndPosition().getOffset(),
        line + System.getProperty("line.separator"), 
        null);
    

    This inserts the new line at the end of the document and appends a line separator.