1 Attachment(s)
Data from .txt to JTable?
Hello. I'm new to Java and was tasked to create a program which reads data from a .txt file and displays it into a JTable. I already know how to create a JTable with hardcoded data.
The thing is, I don't know how to read and separate the inputs so that I can put them in the JTable.
How should I modify my code?
The text file will have data that looks something like this:
Code :
2004-55334, John A. Smith, BSCS, M, CAS
2003-11235, Krista B. Spanner, BAEL, F, CDC
And I'll need to output it into a table which would roughly look like this:
Attachment 760
Here is my current code:
Code :
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class myJTable {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = { { "1-1", "1-2", "1-3","1-4","1-5" },
{ "2-1", "2-2", "2-3","2-4","2-5" } };
Object columnNames[] = { "Student Number", "Name", "Course","Sex","College" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(600, 150);
frame.setVisible(true);
}
}
Re: Data from .txt to JTable?
Your text file is CSV "Comma Separated Values" - it's a well-used convention for storing simple data. You'll find a lot of programs have built-in support for CSV. You could open that text file with LibreOffice Calc - a spreadsheet application - and it would nicely separate the data into cells in the spreadsheet.
To change your code from static data to reading a file line-by-line and splitting the lines on commas, look at the API docs for java.io.BufferedReader (there's an example of *exactly* the line you need to open your file in the intro text) and its readLine() method. Splitting strings on commas is accomplished by the java.lang.String.split() method.
Re: Data from .txt to JTable?
Thanks for replying. I've managed to do what you said about reading the file and splitting the strings. But I don't know how I am to fill the table with the split strings.
This is what I did in order to read lines from the file and split the strings.
Code :
try{
FileReader fReader = new FileReader("Y1L.txt");
BufferedReader inFile = new BufferedReader(fReader);
String input = inFile.readLine();
String[] temp;
temp = input.split(",");
while(input!=null)
{
temp = input.split(",");
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
}
input = inFile.readLine();
}
}catch(Exception e){
System.out.println("ERROR")
}
Re: Data from .txt to JTable?
You do it more or less the same way you did in your first post. Only this time, you don't know in advance the size of the arrays, because you're reading from a file. You should probably explicitly state in your code the width of the data (how many fields on a line) in the file.
There's another String.split() method that you can use to ensure you get no more than a specified number of fields from each line. Then your problem is that you don't know in advance how many rows there are. You could try using the JTable(Vector, Vector) constructor - a Vector is a resizable data structure which grows as you add stuff to it. The API docs again have pretty much everything you need, but basically your two vectors are a Vector of rows and a Vector of column headings. Each element in the Vector of rows is itself a Vector of fields, so you'll need to create instances of Vector from your array from String.split() before adding them to the Vector-of-Vectors.