GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Tuesday, December 21, 2010

Swing - Load File To JTextArea

Here's a code on how to load a text file to a JTextArea Component:

import java.io.*;
import javax.swing.*;

public class Main{

    public static void main(String[] args){

        JFrame frame = new JFrame("JTextArea Application");
        JTextArea textArea = new JTextArea();

        String text = "";
        try{
            FileReader fr = new FileReader("D://data.txt");
            BufferedReader reader = new BufferedReader(fr);
            String textLine = "";
            while(textLine!=null){
                textLine = reader.readLine();
                text = text + textLine + "\n";
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }

        textArea.setText(text);

        frame.add(new JScrollPane(textArea));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Share/Bookmark

No comments:

Post a Comment