Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Data from .txt to JTable?

  1. #1
    Junior Member skywire_'s Avatar
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    2
    Thanked 1 Time in 1 Post

    Post 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:
    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:
    Output.jpg

    Here is my current 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);
     
      }
    }

  2. The Following User Says Thank You to skywire_ For This Useful Post:

    trchinh_it (May 1st, 2013)


  3. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  4. The Following 2 Users Say Thank You to Sean4u For This Useful Post:

    skywire_ (September 23rd, 2011), trchinh_it (May 1st, 2013)

  5. #3
    Junior Member skywire_'s Avatar
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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.
     	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")
    	}

  6. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  7. The Following User Says Thank You to Sean4u For This Useful Post:

    skywire_ (September 23rd, 2011)

Similar Threads

  1. Data is covered by latter data
    By ljz1031 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 15th, 2011, 09:04 PM
  2. Replies: 2
    Last Post: June 15th, 2011, 04:49 PM
  3. Replies: 1
    Last Post: June 11th, 2011, 06:39 AM
  4. JTable
    By timmin in forum AWT / Java Swing
    Replies: 3
    Last Post: April 3rd, 2011, 01:10 PM
  5. Hello everyone! I need help with JTable
    By hektor in forum AWT / Java Swing
    Replies: 6
    Last Post: October 11th, 2010, 11:11 AM