how to import csv data to a jTable

D

dsilvawilliam

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
 
M

M.J. Dance

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...ble(java.lang.Object[][], java.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top