Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

how to import csv data to a jTable

Thread view: 
dsilvawilliam@gmail.com - 28 Aug 2006 13:44 GMT
Hi All;
        I am trying to get csv values into a jTable can anyone hlp me
by sending a sample code or even a code of reading a text file on a
java interface......thanks.....william
Andrew Thompson - 28 Aug 2006 14:15 GMT
> Hi All;

Please refrain from multi-posting.

(X-posted to c.l.j.h,p,g w/f-ups to c.l.j.h.)

Andrew T.
M.J. Dance - 28 Aug 2006 14:22 GMT
> Hi All;
>          I am trying to get csv values into a jTable can anyone hlp me
> by sending a sample code or even a code of reading a text file on a
> java interface......thanks.....william

import java.io.*;
//import java.util.*;
import javax.swing.*;

public class HWGuideline {
    public static void main(String[] args) {
/*   
First you need a filename. Having that, you use FileReader (
http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileReader.html ) to read it.
Now, as CSV file is usualy organized as one-row-per-record, wrapping FileReader
with BufferedReader (
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html ,
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine() )
would come in handy. Being able to read one line/record at a time, you now need
a means of splitting a record into columns/fields. What you need is either a
StringTokenizer (
http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html ) or a
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
 with a proper regex (^\\t$ perhaps?). Now you pack so obtained data into a two
dimensional array of String[rows][cols] (plus an additional one-dimensional one
with column names) and, feeding that to JTable constructor (
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html#JTable(java.lang
.Object[][],%20java.lang.Object
[])
), you have an "operational" JTable.
*/
    }
}

You should be able to write the actual code yourself. Otherwise, start here:
http://java.sun.com/j2se/1.5.0/docs/index.html
http://java.sun.com/docs/books/tutorial/index.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.