Loading a tab delimited file into JTable

Posted on 2005-04-14
Last Modified: 2012-06-21
      I want to read tab delimited data from a file into a JTable, line by line.First line is for columns. However i'm having difficulty using the split function of String.I am unsure what to use because  split("\\t") or split(" ") causes the data to load improperly. How can i achieve this?
Question by:D0cX
    LVL 86

    Expert Comment

    You need to make a TableModel

                      Vector allRows = new Vector();
                      // for each line:
                      allRows.add(new Vector(Arrays.asList(line.split("\\s*?"))));
                            // then
                      JTable table = new JTable(new DefaultTableModel(allRows));
    LVL 86

    Expert Comment

    Forgot this:

    // (for first line)
    Vector headers = new Vector(Arrays.asList(line.split("\\s*?")));

    // then

     JTable table = new JTable(new DefaultTableModel(allRows, headers));

    Author Comment

    Hey thanks for the help.
    It produced more desirable results , however for a file with data

    col1 col2 col3 col4
    dat1 dat2 dat3 dat4

    i get a leading empty column before the first data column and blank columns in between data columns. Any ideas on how to refine this?
    what does "\\s*?" do anyway?
    LVL 86

    Expert Comment

    Hmm. Any chance of posting part of the file somewhere publicly downloadable?

    >>what does "\\s*?" do anyway?

    Tokenizes on whitespace
    LVL 15

    Accepted Solution

    You can try:

    bsh % Arrays.asList("    col1 col2 col3    col4   ".trim().split("\\s+"));
    <[col1, col2, col3, col4]>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now