Results 1 to 6 of 6

Thread: JTable

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

    Default JTable

    Java 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);
        }
    }
    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your data Vector is actually a Vector of Vectors and should be treated as such. For each row of data, create a Vector<Object> and fill it with the parsed information, and then after the row is done (done with the current line), add it to the data Vector<Vector<Object>>. You're also going to have to put more thought into your parsing of the data file. The first line contains a String representation of a date, but you're reading it as if it has column information and trying to parse it with "|". That's obviously not going to work. And you'll need to revise your getRowCount() to reflect the changes in the data structure.

    Much luck.
    Last edited by Fubarable; 04-27-2010 at 04:48 AM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Your data Vector is actually a Vector of Vectors and should be treated as such.
    It looks like the custom model is actually written to support the data in a single Vector. Its not the way the DefaultTableModel works, but I don't see a problem assuming it is implemented correctly.

    (e.g [Bodeplot.dat] and 4/18/10) are not included in the Jtable but rather are just lines above it
    Well, its not a good idea to include "comments" with your actual data. But if you want to do this then you need to parse each line you read in to see if the data is a comment or not. Maybe a simple idea is to start each comment line with some special identifier like "//". Then you need to modify your code to create a loop to read the comments at the start of the file. Once all the comments are read you can assume the next line is your header line and the other lines are the data lines.

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

    Default

    Thanks for your replies. I don't want to use a character to identify the comment. I have a date as part of the file so I want to be able to read the date as not part of the JTable but as a line before the actual Jtable begins. I don't want to place a character before the comment each time.

    In response to the earlier comment by Fubarable, I don't really understand what he/she meant by
    Your data Vector is actually a Vector of Vectors..... And you'll need to revise your getRowCount() to reflect the changes in the data structure.
    How do i differentiate get my code to distinguish between statements that possess a delimter and statements that do not. As I want the latter to be displayed in a table. Any help is much appreciated.

    Sorry for the trouble.
    Last edited by BeeGee; 04-27-2010 at 08:53 PM.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I have a date as part of the file
    Well, you are in control of the format of the input file. If you want the first line to be a date, then it can be a date. So you change your code so that:

    a) read one line and assume it contains a date
    b) read the second line and assume it contains the headings for the table
    c) read the rest of the file and assume each line is a row in the table.

    I was giving you a more "generic" solution so that you could have multiple dates or other pieces of information if you desired.

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

Similar Threads

  1. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 09:27 AM
  2. JTable help
    By FFEJ in forum New To Java
    Replies: 5
    Last Post: 10-06-2009, 12:30 AM
  3. Regarding the JTable
    By suresh.uvce in forum AWT / Swing
    Replies: 5
    Last Post: 02-11-2009, 07:46 AM
  4. Row Name In Jtable
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 08-16-2008, 01:23 AM
  5. Replies: 2
    Last Post: 05-15-2008, 11:15 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
  •