Forums Register Login

displaying the datas in JTable

+Pie Number of slices to send: Send
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);

}
}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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 .

+Pie Number of slices to send: Send
You're welcome.
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1083 times.
Similar Threads
JTable
JTable ........
JTable
Image in JTable
JTable and getValueAt()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:14:14.