java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4090
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Read a data file into a JTable E-mail
User Rating: / 30
PoorBest 

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(400300);
    }
    
    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);
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.