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 / GUI / August 2005

Tip: Looking for answers? Try searching our database.

simple jtable that shows content of a tab-delimited file

Thread view: 
les_ander@yahoo.com - 19 Aug 2005 19:05 GMT
Hi,
I want to simply read a file which has tab delimited data (each row
will have the same number of columns).
I have the following code but it does not work.
Can someone please help,
thanks

/*

read a datafile that is pipe or tab delimited

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

class JTableViewer
{
    static JTable table;
    static DefaultTableModel model;

    static Vector readFile(String filename){
        // model=new DefaultTableModel();
        Vector V=new Vector();
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            String str;
            int nRows=0;
            while ((str = in.readLine()) != null) {
                nRows+=1;
                // model.insertRow( model.getRowCount(), str.split("\\t"));
                V.add(new Vector(str.split("\\t")));
            }

            in.close();
            System.err.println("Read "+nRows+" lines.");
        } catch (IOException e) {
            System.err.println("problem reading this file");
            System.exit(-1);
        }
        return(V);
    }

    public static void main(String args[]) //throws IOException
    {
        String filename="";
        if (args.length < 1){
            System.err.println("Need one argument: tab-delimited file name");
            System.exit(-1);
        }else{

            filename=args[0];
            System.err.println("Reading: " + filename);
        }
        JFrame frame=new JFrame("View Tab-delimited files");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().setLayout(new BorderLayout());
        frame.setSize(500,500);
        Vector V=readFile(filename);
        Vector ColumnNames=new Vector(new Object[]{"First","Second"});
        table=new JTable(V,ColumnNames);
        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        frame.getContentPane().add( topPanel );
        table=new JTable(model);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        topPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
Vova Reznik - 19 Aug 2005 19:42 GMT
> import java.util.*;
> import javax.swing.*;
[quoted text clipped - 17 lines]
>                 nRows+=1;
>                 // model.insertRow( model.getRowCount(), str.split("\\t"));

>                 V.add(new Vector(str.split("\\t")));
Vector doesn't have constructor that accepts String[]
Did yoy compile your code?
String[]record = str.split("\\t");
put each element into vector and when put the vector into V
Default model understands data as Vector of Vector, there each record
is a Vector and each cell is element of vector
>             }
>
[quoted text clipped - 24 lines]
>         Vector V=readFile(filename);
>         Vector ColumnNames=new Vector(new Object[]{"First","Second"});
Are you sure you will have only 2 columns?
>         table=new JTable(V,ColumnNames);
Creating table from vectors. OK
>         JPanel topPanel = new JPanel();
>         topPanel.setLayout( new BorderLayout() );
>         frame.getContentPane().add( topPanel );
>         table=new JTable(model);
Recreating table!!! Hey where is data? Your model is null (commented
everythere)
>         table.setPreferredScrollableViewportSize(new Dimension(500, 70));
>         topPanel.add(new JScrollPane(table), BorderLayout.CENTER);
>         frame.setVisible(true);
>     }
> }

Name of variable in java should start from lwercase letter

Before sending examples - try to find problem by yourself.
At least try to compile and read messages
Pete Barrett - 19 Aug 2005 20:46 GMT
>Hi,
>I want to simply read a file which has tab delimited data (each row
>will have the same number of columns).
>I have the following code but it does not work.

I think you have to admit that "it does not work" isn't very specific!
What exactly does it not do that it should? Does it fail to compile?
Is it not displaying the table? Is it displaying the table
incorrectly? Is it not loading the data?

However...
>...
>    static Vector readFile(String filename){
[quoted text clipped - 8 lines]
>                // model.insertRow( model.getRowCount(), str.split("\\t"));
>                V.add(new Vector(str.split("\\t")));
String.split() returns an array of StringS. There's no constructor of
Vector which takes an array. There *is* one which takes a Collection,
but arrays don't implement the Collection interface, so I'd be
surprised if this compiles.

What you probably want to do is to do the split, then iterate through
the StringS which result, adding each to a Vector (which represents a
row), then add that Vector to the original Vector (which represents
all the data in a DefaultTableModel.

Pete Barrett
Andrew Thompson - 19 Aug 2005 21:00 GMT
>>I want to simply read a file which has tab delimited data (each row
>>will have the same number of columns).
>>I have the following code but it does not work.
>
> I think you have to admit that "it does not work" isn't very specific!

Maybe it got unionised, and is on strike for better
conditions!  ..Or maybe it is just lazy.   ;-)

[ ducks ]

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"The Milky Way Express is loading.  All aboard."
Jimi Hendrix 'Stars That Play With Laughing Sam's Dice'

Aki Laukkanen - 22 Aug 2005 10:01 GMT
>>>I want to simply read a file which has tab delimited data (each row
>>>will have the same number of columns).
[quoted text clipped - 6 lines]
>
> [ ducks ]

Damn those lazy commie ducks! :)

Signature

-Aki "Sus" Laukkanen
Kirjoittaja on itseoppinut koodaava sekatyömies ja pientilallinen.
The writer is a self-educated coding handyman and a small-time farmer.



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.