简体   繁体   中英

Trying to read from a text file to display into a JTextArea using a BufferedReader

So I am trying to read a text file to display into a JTextArea I am having no luck getting it to show up. I believe the problem lies around the setup of my BufferedReader but I am not certain. It also may be a problem with my SOP section. Any help is appreciated.

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class WindowInventory extends JFrame {

    private JTextArea ta_inventory;

    public WindowInventory() {
        setSize(200, 500);
        setResizable(false);
        setTitle("Inventory");
        setLocation(200, 200);

        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);

        ta_inventory = new JTextArea();
        ta_inventory.setBounds(0, 0, 200, 350);
        contentPane.add(ta_inventory);

        try {

            BufferedReader myReader = new BufferedReader(new FileReader("Inventory.txt"));

            while (myReader.ready()) {

                String Name = myReader.readLine();
                System.out.println(Name);
                String Price = myReader.readLine();
                System.out.println(Price);
                String ID = myReader.readLine();
                System.out.println(ID);

            }

            myReader.close();

        } catch (IOException e) {

            System.out.println("Problem reading from a file");

        }
    }
}

Yes, your method of setting up the reader is wrong. Take a look at Basic I/O for more deatils.

However, you could try using something more like...

try (BufferedReader myReader = new BufferedReader(new FileReader("Inventory.txt"))) {
    String text = null;
    while ((text = myReader.readLine()) != null) {
        System.out.println(text);
    }
} catch (IOException exp) {
    exp.printStackTrace();
}

Instead.

Now having said that. It would be easier to simple use JTextArea#read

Something like....

JTextArea textArea = new JTextArea(10, 20);
try (Reader myReader = new BufferedReader(new FileReader("Inventory.txt"))) {
    textArea.read(myReader, "Inventory");
} catch (IOException exp) {
    exp.printStackTrace();
}

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Text components like JTextArea really should be maintained within a JScrollPane . See How to Use Scroll Panes and How to Use Text Areas for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM