This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying the datas in JTable

 
Thamu Gurung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Thamu Gurung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic