Hello, I was tasked to make a program that reads data from a text file and then place it into a JTable. I managed to read the file and split the strings but I don't know how to put the strings into a format accepted by JTable.
Please help. This is my code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.io.FileReader;
import java.io.BufferedReader;
public class myJTable {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Splitting Strings
try{
FileReader fReader = new FileReader("accounts.txt");
BufferedReader inFile = new BufferedReader(fReader);
String input = inFile.readLine();
String[] temp;
//String[][] temp2 = new String[fReader.length][];
temp = input.split(",");
while(input!=null)
{
int x=0;
temp = input.split(",",5);
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
}
System.out.println("---------End of Line");
input = inFile.readLine();
}
}catch(Exception e){
System.out.println("ERROR");
}
//endOfStringSplit
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", "Sex","College","Username","Password" };
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(600, 150);
frame.setVisible(true);
}
}