File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes displaying the datas in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "displaying the datas in JTable" Watch "displaying the datas in JTable" New topic
Author

displaying the datas in JTable

Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
hi everybody ,

I really appreciate if you could help me in displaying the datas of .txt file in JTable .....my codes are below ..really new to JAVA so pleaese help

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTable;
import java.io.*;

public class DisData{

public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedReader r=new BufferedReader(new FileReader("Data.txt"));
String line =r.readLine();
while (line!=null){
Object rowData[]=line.split(","); // having error at this point

}

Object columnNames[] = { "Name", "Address", "Mobile" };

JTable table = new JTable(rowData, columnNames);


JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);

}
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Do you know anything about this error at all? If not, then your first task is to find out something. An error message would be useful. Finding out whether it occurs at compile time and run time would be helpful too. As you will see if you think about it for a while, it's pretty difficult for somebody to fix a problem when they know almost nothing about it.
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
the error message is like this

cannot find symbol
symbol : variable rowData
JTable table = new JTable(rowData, columnNames);

actually when the applciation runs i want the datas of Data.txt to be displayed in JTable automatically..is it possible...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Please UseCodeTags next time. It will be easier to read.

The problem is that your rowData variable is local to the while-loop only. Afterwards it no longer exists. Also, the JTable constructor requires an Object[][], not an Object[].

Switch to using a DefaultTableModel instead. It has methods that allow you to add rows on the fly:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thamu Gurung
Ranch Hand

Joined: Feb 02, 2010
Posts: 39
Thankyou Rob, for helping out , it was really nice . As I am new in JAVA and also new here , so next time I'll make proper use of the codes as per your suggestion.

Thankyou once again , now its working and even after that I went through the API and now I am able to add some options to JTable like sorting and other things .

Really nice to be here .

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying the datas in JTable
 
Similar Threads
JTable ........
JTable
Image in JTable
JTable
JTable and getValueAt()