将文本加载到JTable时如何显示空列?

时间:2012-08-10 02:41:05

标签: java swing file-io jtable abstracttablemodel

我需要使用JTable将.txt加载到delimiter,我找到了一个很好的样本here

这是样本数据:

 102|Beth Reiser||New York|(212)5558725
 111|Dylan Ricci||Syracuse|(315)5554486
 116|Brian Gugliuzza||Mamaroneck|(914)5553817
 120|Gertrude Stein||Elmsford|(914)5553476
 131|Daljit Sinnot||Bohemia|(516)5559811

这是我修改后的代码:

package Model;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

public class DataFileTableModel extends AbstractTableModel {

    protected Vector data;
    protected Vector columnNames ;
    protected String datafile;

    public DataFileTableModel(String f, String delimiter){
        datafile = f;
        initVectors(delimiter);
    }

    public void initVectors(String delimiter) {

        String aLine ;
        data = new Vector();
        columnNames = new Vector();
        int lineNum=0;

        try {
            FileInputStream fin =  new FileInputStream(datafile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            // extract column names
            StringTokenizer st1 =
                    new StringTokenizer(br.readLine(), delimiter);
            while(st1.hasMoreTokens())
                columnNames.addElement(st1.nextToken());
            // extract data
            while ((aLine = br.readLine()) != null && lineNum<20) {
                StringTokenizer st2 =
                        new StringTokenizer(aLine, delimiter);
                lineNum++;
                while(st2.hasMoreTokens())
                    data.addElement(st2.nextToken());
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getRowCount() {
        return data.size() / getColumnCount();
    }

    public int getColumnCount(){
        return columnNames.size();
    }

    public String getColumnName(int columnIndex) {
        String colName = "";

        if (columnIndex <= getColumnCount())
            colName = (String)columnNames.elementAt(columnIndex);

        return colName;
    }

    public Class getColumnClass(int columnIndex){
        return String.class;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        return;
    }
}

根据数据,它将显示5列,但它只显示4列,因为空列而跳过列3。我想将所有列显示到JTable,我怎么能实现这一目标?根据@ hovercraft-full-of-eels,当空列位于表的中间时,它会成功显示,但它无法处理背面的空列。怎么弄这个?

背面空列的数据样本:

102|Beth Reiser||New York|(212)5558725||||
111|Dylan Ricci||Syracuse|(315)5554486||||
116|Brian Gugliuzza||Mamaroneck|(914)5553817||||

2 个答案:

答案 0 :(得分:3)

这完全归功于你使用StringTokenizer:你的标记器正在跳过空列纯粹而简单。首先你应该简化你的问题 - 测试StringTokenizer解析文本文件 而不用 任何Swing代码,然后尝试修复 之前 添加任何Swing代码或制作JTable。

使用StringTokenizer查看测试数据的结果与\\|上的拆分结果:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class DataFileFoo {
   private static final String DATA_FILE_PATH = "datafile.txt";
   private static final String DELIMITER = "|";
   private static final String SPLIT_REGEX = "\\|";

   public static void main(String[] args) {
      System.out.println("usingStringTokenizer()");
      usingStringTokenizer();

      System.out.println();
      System.out.println("usingStringSplit();");
      usingStringSplit();
   }

   public static void usingStringTokenizer() {
      File datafile = new File(DATA_FILE_PATH);
      try {
         FileInputStream fin = new FileInputStream(datafile);
         BufferedReader br = new BufferedReader(new InputStreamReader(fin));
         // extract column names
         String aLine = "";
         while ((aLine = br.readLine()) != null) {
            StringTokenizer st2 = new StringTokenizer(aLine, DELIMITER);
            while (st2.hasMoreTokens()) {
               System.out.print(st2.nextToken() + ", ");
            }
            System.out.println();

         }
         br.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void usingStringSplit() {
      File datafile = new File(DATA_FILE_PATH);
      try {
         FileInputStream fin = new FileInputStream(datafile);
         BufferedReader br = new BufferedReader(new InputStreamReader(fin));
         // extract column names
         String aLine = "";
         while ((aLine = br.readLine()) != null) {
            String[] tokens = aLine.split(SPLIT_REGEX);
            for (String token : tokens) {
               System.out.print(token + ", ");
            }
            System.out.println();

         }
         br.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

使用您的数据文件

 102|Beth Reiser||New York|(212)5558725
 111|Dylan Ricci||Syracuse|(315)5554486
 116|Brian Gugliuzza||Mamaroneck|(914)5553817
 120|Gertrude Stein||Elmsford|(914)5553476
 131|Daljit Sinnot||Bohemia|(516)5559811

返回:

usingStringTokenizer()
 102, Beth Reiser, New York, (212)5558725, 
 111, Dylan Ricci, Syracuse, (315)5554486, 
 116, Brian Gugliuzza, Mamaroneck, (914)5553817, 
 120, Gertrude Stein, Elmsford, (914)5553476, 
 131, Daljit Sinnot, Bohemia, (516)5559811, 

usingStringSplit();
 102, Beth Reiser, , New York, (212)5558725, 
 111, Dylan Ricci, , Syracuse, (315)5554486, 
 116, Brian Gugliuzza, , Mamaroneck, (914)5553817, 
 120, Gertrude Stein, , Elmsford, (914)5553476, 
 131, Daljit Sinnot, , Bohemia, (516)5559811, 

答案 1 :(得分:-1)

列的大小可能是0px。要查看专栏,为什么不在列中填写空格字符' '