Program to read the text from a file and display it on a JFrame.

Ads
 

Program to read the text from a file and display it on a JFrame.

import javax.swing.*;
import java.io.*;
import java.lang.*;
import java.awt.*;
class MegaViewer1 extends JFrame
{
    JTabbedPane jtp1=new JTabbedPane();
    JPanel jp1=new JPanel();
    JTextArea t1=new JTextArea();
    MegaViewer1() throws Exception
    {
        super("MegaViewer");
        FileReader f=new FileReader("Eight.java");
        BufferedReader brk=new BufferedReader(f);       
        String s;
        while((s=brk.readLine())!=null)
        t1.setText(s);
        jp1.add(t1);
        jtp1.addTab("Tab1",t1);
        add(jtp1);  
    }
    public static void main(String args[]) throws Exception
    {
        MegaViewer1 mv1=new MegaViewer1();
        mv1.pack();
        mv1.setVisible(true);   
        mv1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }
}

The issue with this is only the last character is been displayed on the tabbed frame(i.e a "}"). Any suggestions ?

View Answers

May 3, 2012 at 11:31 AM

Use append() method instead of setText().

Here is your modified code:

import javax.swing.*;
import java.io.*;
import java.lang.*;
import java.awt.*;
class MegaViewer1 extends JFrame
{
    JTabbedPane jtp1=new JTabbedPane();
    JPanel jp1=new JPanel();
    JTextArea t1=new JTextArea();
    MegaViewer1() throws Exception
    {
        super("MegaViewer");
        FileReader f=new FileReader("A.java");
        BufferedReader brk=new BufferedReader(f);       
        String s;
        while((s=brk.readLine())!=null){
        t1.append(s);
        }
        jp1.add(t1);
        jtp1.addTab("Tab1",t1);
        add(jtp1);  
    }
    public static void main(String args[]) throws Exception
    {
        MegaViewer1 mv1=new MegaViewer1();
        mv1.pack();
        mv1.setVisible(true);   
        mv1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }
}

Ads









Related Tutorials/Questions & Answers:
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  ...*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane... java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1
java program to read multiple files from a directory and display them on a jframe
java program to read multiple files from a directory and display them on a jframe  hi guys i want to write a java program that will read all files in a directory and display them one by on on a jframe on jpanel with specific key
Advertisements
Read Lines from text file
read from the text file and displays the output as desired. Unable to read the rest...Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating... and we want to copy the content into another text file from our Java program... and the class is used to read data from a file. The could be text file
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
Read File from specified path in Java
the file, the program will read text data line by line and print on the console... of the program that reads a text file from the disk path and print on the console...How to read a file from a specified path in Java? After learning so many
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text File  I need to read a file that was selected by the user using... to display the names and path on the console. But I can not get the file to read line
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
Steps to read text file in pyspark
Steps to read text file in pyspark  Hi, I am learning to write program in PySpark. I want to simply read a text file in Pyspark and then try some code. What are the Steps to read text file in pyspark? How much time it takes
display multiple images from postgres database in jframe
display multiple images from postgres database in jframe  i just want to display multiple images on jframe by firing a query on postgres database
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
Read text file in PySpark
Read text file in PySpark - How to read a text file in PySpark? The PySpark... configuration conf = SparkConf().setAppName("read text file in pyspark") sc... context with Spark configuration conf = SparkConf().setAppName("read text file
How to read the .doc/ .docx file in Java Program
the text from the word file as an array and display the data on word file...How to read the .doc/ .docx file in Java Program  Hi, I am beginner in Java programming language. Can anybody explain How to read .doc file in Java
How to create file from input values in Jframe ?
How to create file from input values in Jframe ?  hi i m doing my... the inputs given by the user.i had a java program for it and accessed it by creating an object for it.my java code is public class submit { JFrame frame; JTextArea
Display non-duplicate words from file
Display non-duplicate words from file In this tutorial, you will learn how to read a text file and display non-duplicate words in ascending order. The given code reads the file and stored the file text into StringBuffer. The buffer
Read Property file From Jmeter
Read Property file From Jmeter  Hi, I am running web driver script from Jmeter, but while reading property file I am getting "File Not Find Exception". Please tell me how to read ".properties" file from Jmeter
Java read text file
by other program. Here we have used DataInputStream class to read the text file... a text file one line at a time. It can also be used to read large text files.... Here is the video insturction "How to read text file in Java?"
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file... want to displaythe above .txt file in jtable as following format having 3 columns contigID length size and then display sequence like "ATGCGSA..." in text
Java read lines from file
Java read lines from file  Any code example related to Java read lines from file? In my project there is requirement of reading the file line by line... to read the whole file in one go. So, in my case reading file line by line is only
I am creating one jsp page in which I read in a text file, then display that data in tabular format. Now I need to calculate a total.
I am creating one jsp page in which I read in a text file, then display that data in tabular format. Now I need to calculate a total.  I am reading the file using BufferedReader, then writing the output like this: String
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access... me the things where i went wrong java servlet program protected void... ServletException, IOException { response.setContentType("text/xml"); FileRead fr
stand alone program for text file editing
stand alone program for text file editing  steps 1.read a text file origi.txt from a location 2.make a copy of the text file to the same location with name as origi_copy.txt(example) 3.each line of the copy should start from 10
How to Read file line by line in Java program
to read a file with name ?Read file line by line in Java.txt? that includes a text...() method is invocated, they can convert the bytes read from the file... required as it can directly reads the number from the file. Example of Read file
how read data from doc file in same formate in jsp
how read data from doc file in same formate in jsp  how we can read and display data on jsp page, from doc file with the same formatting
how to sort the text file and display it in jtable
how to sort the text file and display it in jtable   my text file is: contig00001 length=586 numreads=4... ctgaggactcctcg i want to display the lines started with ">" symbol to be displayed
read from cmd syntax and open the file
read from cmd syntax and open the file  i have to read the name of the database e.g., "database.txt" file from a cmd syntax and open it: java FindKeys database=db.txt;minjoinrows=integer|all how can i do
read from cmd syntax and open the file
read from cmd syntax and open the file  i have to read the name of the database e.g., "database.txt" file from a cmd syntax and open it: java FindKeys database=db.txt;minjoinrows=integer|all how can i do
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been assigned a work to read big text file and extract the data and save into database... memory assigned is also a limit. So, we have decided to read the text file line
read from file and store using hash map
read from file and store using hash map  I was stuck with a java project where I have to read a file with two different concepts and store them differently in hashmap. My data file would be something like Adults: Name, xyz

Ads