Results 1 to 4 of 4

Thread: File to Jtable

  1. #1
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default File to Jtable

    Hello,

    I randomly came about this code on Java Tips - Read a data file into a JTable.
    I understand the code and what it does. However I was trying to modify it and was not as successful as I had hoped.
    The code basically reads a file and transports it into a JTable. Howver, it only addresses the issue of having one row of Column names (don't know if that makes any sense).
    For instance, I was trying to make it read a file like this:

    [BodePlot.dat]
    Frequency|Magnitude|Phase
    f (Hz)| MdB = 20 log M|φ = τφ ω
    10|-102|0
    50| -106|-41
    100| -109| -66
    150| -112| -81
    200| -114| -89

    XML Code:
    //[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("BodePlot.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);
        }
    }
    I am writing to find out how to address the second line that's not actual data. How do I get that line to show up as part of the column heading on the JTable.

    Thanks for your help.
    Last edited by BeeGee; 04-10-2010 at 04:13 AM. Reason: Accidentally posted it.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Well, instead of doing it dynamically you have to first figure out how to do it with static data. Once you solve that then you will know how to do it with data from a file.

    A JTableHeader does not support multiple lines of text. However I think you can use HTML to you would need to build a string that looks like:

    String column1 = "<html>line1<br>line2</html>";

    Assuming that works you then modify you code to to read the first two lines of data and build the columns names in that format.

  3. #3
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    Thanks for your help but How do i work that into the code?
    Thanks.

  4. #4
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    With regards to the above code, I have encountered another problem. When I modified the table as shown below, the JTable did not display rows and columns with respect to the delimiter (|); instead, it displayed all the data in rows.

    [BodePlot.dat]
    4/18/10
    Frequency|Magnitude|Phase
    f (Hz)| MdB = 20 log M|φ = τφ ω
    10|-102|0
    50| -106|-41
    100| -109| -66
    150| -112| -81
    200| -114| -89

    So for instance, when i added a few lines above the table that i wanted to display in the JTable, a bunch of rows with the data was shown in the Jtable.

    I am writing to request assistance, wrt code above, on how to modify the code so that the data with the delimiter(|) only gets written in a Jtable format and all the comments (e.g [Bodeplot.dat] and 4/18/10) are not included in the Jtable but rather are just lines above it.

    I hope this is not confusing. thanks in advance for your help.

Similar Threads

  1. Reading from file and adding to JTable
    By purple in forum New To Java
    Replies: 11
    Last Post: 04-14-2011, 10:33 PM
  2. .txt file into JTable
    By Hikari in forum New To Java
    Replies: 11
    Last Post: 12-09-2010, 12:25 AM
  3. JTable to txt File
    By Tzaphiel in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 11:47 AM
  4. want to save JTable in a file..
    By doha786 in forum New To Java
    Replies: 4
    Last Post: 04-10-2010, 08:37 AM
  5. JTable, Vector, File, Problem.
    By ocean in forum New To Java
    Replies: 4
    Last Post: 11-06-2009, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •