1,105,386 Community Members

Data from text file into JTable?

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
 
  }
}
Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
1
 

check this thread

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Um.. I'm not sure how to use the vectors in my case. >_<

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

A Vector is like an array, except that it starts out with size 0, and grows to fit the data you put into it - you just keep using its add method.
You can also have a Vector whose elements are also Vectors - like a 2 D array. JTable has a constructor that takes a Vector of Vectors as a parameter to supply all the data in the table. Each "outer" Vector represents on row, and the "inner" Vectors have the individual cell values for that row.
So instead of reading your data into an array, read each line and and add it to a new Vector<String>, then add that Vector to a Vector<Vector<String>> which you can pass directly to JTable.

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It says the Vector<Vector<String>> is undefined. What should I do?

[EDIT] Never mind. I got it. Thanks for the help guys!

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm having a bit of trouble with displaying the data. The JTable only displays the first line of the file repeatedly. It doesn't display the others. But when I print the vector, it prints all of the data properly. What's wrong with it?

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Vector;

public class myJTable {

  public static void main(String args[]) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<String> v = new Vector<String>();
    Vector<Vector<String>> myVector = new Vector<Vector<String>>();

 	try{
           FileReader fReader = new FileReader("accounts.txt");
           BufferedReader inFile = new BufferedReader(fReader);
           String input;
	   String[] temp;

	//String[][] temp2 = new String[fReader.length][];

		while((input=inFile.readLine())!=null)
		{
			temp = input.split(",",6);
			for(int i=0;i<temp.length;i++){
				v.add(temp[i]);
				System.out.println(temp[i]+" added");
			}
			System.out.println("V is "+v);

			myVector.add(v);

			System.out.println("---------End of Line");

				Vector<String> columnNames = new Vector<String>();
 			   	columnNames.addElement("Student Number");
    				columnNames.addElement("Name");
                                columnNames.addElement("Sex");
    				columnNames.addElement("College");
    				columnNames.addElement("Username");
    				columnNames.addElement("Password");
				JTable table = new JTable(myVector,columnNames);
     			        JScrollPane scrollPane = new JScrollPane(table);
    			        frame.add(scrollPane, BorderLayout.CENTER);
    			        frame.setSize(600, 150);
    			        frame.setVisible(true);

		}
	}catch(Exception e){
		System.out.println("ERROR");
	}
  }

}
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Looks like you keep adding all the data to the vector v, but you need to clear that down (or start a new vector) for each line.
Also you are creating the table inside your read loop, so you create new one each time ou read a line, but you should do this just once, after the loop

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

how many Object you created in each loop

you must to remove

JTable table = new JTable(myVector,columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(600, 150);
frame.setVisible(true);

and move these code lines outside try-catch code block

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok so I've moved the codes you asked. It only prints out the first line three times.

This is what's inside 'accounts.txt' for reference.

2010-12345,John A. Smith,M,CAS,joas1,password
2011-12346,Joan B. Smith,F,CDC,jobs1,password
2012-12347,John C. Smith,M,CEM,joas2,password

it only shows '2010-12345,John A. Smith,M,CAS,joas1,password' three times.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Did you read my post from 6 days ago? The one that starts "Looks like you keep adding all the data to the vector v, but you need to clear that down (or start a new vector) for each line."?

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Darn. Didn't see it. How should I clear the vector after adding? Should I set it to null?

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

I would just create a new one for each line

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

Got more time now - so a better answer:
You are trying to create a Vector of Vectors, the "inner" Vectors representing individual rows. This means you MUST create a new Vector for each row. If you add the Vector for the first row, then clear its contents, you will lose the data for the first row.

Member Avatar
skywire_
Newbie Poster
7 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I got it now. I just had to recreate the vector of strings after I've added the first one to the vector of vectors. Thank you guys for the help! :D

Question Answered as of 2 Years Ago by JamesCherrill and mKorbel
Member Avatar
Eternlo7
Newbie Poster
2 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello, I have similar problem like you...except that you already solve it..

For starter, I have copied all the coding you've done and it's working just fine..

except for the part where I need to create a new vector for each row..

Can you kindly show me how you do it..since I'm noob in java..I don't know how to do it..

just for the part when to create a new vector..thanx in advance

Member Avatar
Eternlo7
Newbie Poster
2 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

nevermind, I just did it...you guys are genius..

I've been searching in the internet for hours then when I found this forum...

It just took me 15 minutes..thanx

ps: sorry for my broken english

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

@Eternlo7 please start new thread, your own ..., not hijack 1M old, btw I miss real question in your post

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article