Share this page 

Read a data file into a JTableTag(s): Swing


The first line of the data file contains the column names. Fields are separated by the "|" character.

[customers.dat]

Id|Name|City|Phone
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
First we need a TableModel to define the data structure to used by the JTable.

[DataFileTableModel.java]

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){
    datafile = f;
    initVectors();  
    }

  public void initVectors() {
    String aLine ;
    data = new Vector();
    columnNames = new Vector();
    try {
      FileInputStream fin =  new FileInputStream(datafile);
      BufferedReader br = new BufferedReader(new InputStreamReader(fin));
      // extract column names
      StringTokenizer st1 = 
         new StringTokenizer(br.readLine(), "|");
        while(st1.hasMoreTokens())
          columnNames.addElement(st1.nextToken());
      // extract data
      while ((aLine = br.readLine()) != null) {  
        StringTokenizer st2 = 
         new StringTokenizer(aLine, "|");
        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;
    }
}

[DataFileTable.java]

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

public class DataFileTable extends JPanel {
  public DataFileTable(String dataFilePath) {
    JTable table;
    DataFileTableModel model;
    Font f;

    f = new Font("SanSerif",Font.PLAIN,24);
    setFont(f);
    setLayout(new BorderLayout());

    model = new DataFileTableModel(dataFilePath);

    table = new JTable();
    table.setModel(model);
    table.createDefaultColumnsFromModel();

    JScrollPane scrollpane = new JScrollPane(table);
    add(scrollpane);
    }

 public Dimension getPreferredSize(){
    return new Dimension(400, 300);
    }
    
 public static void main(String s[]) {
    JFrame frame = new JFrame("Data File Table");
    DataFileTable panel;
        
    panel = new DataFileTable("customers.dat");

    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.setForeground(Color.black);
    frame.setBackground(Color.lightGray);
    frame.getContentPane().add(panel,"Center");
        
    frame.setSize(panel.getPreferredSize());
    frame.setVisible(true);
    frame.addWindowListener(new WindowCloser());
    }
 }

class WindowCloser extends WindowAdapter {
 public void windowClosing(WindowEvent e) {
   Window win = e.getWindow();
   win.setVisible(false);
   System.exit(0);
    }
}