5 Replies - 1865 Views - Last Post: 23 June 2012 - 06:40 AM Rate Topic: -----

#1 ankurtyagi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-June 12

Problem Importing data from file to Jtable

Posted 20 June 2012 - 08:28 PM

Sir,
i m trying to develop a application which stores data entered by user in Jtable and save it to a file and when application is ran again then the application can retrive the information from file to show it in jtable.

i know basics of java. but knows little about table api.
i know's about Tablemodel(Table's data) and table.

So to accomplish above task i create a Tablemodel class InsertFileDataToJtable to read data from file as shown below.

public class InsertFileDataToJTable extends AbstractTableModel {
       Vector data;
        Vector columns;

        public InsertFileDataToJTable() {
                String line;
                data = new Vector();
                columns = new Vector();
                try {
                        FileInputStream fis = new FileInputStream("BellTime.file");
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                        StringTokenizer st1 = new StringTokenizer(br.readLine(), "\t");
                        while (st1.hasMoreTokens())
                                columns.add(st1.nextToken());
                        while ((line = br.readLine()) != null) {
                                StringTokenizer st2 = new StringTokenizer(line, "\t");
                                while (st2.hasMoreTokens())
                                        data.add(st2.nextToken());
                        }
                        br.close();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

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

        public int getColumnCount() {
                return columns.size();
        }
        
        public Class getColumnClass(int index)
        {
            return String.class;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
                return (String)data.get((rowIndex * getColumnCount())
                                + columnIndex);
        }
        
        
        public String getColumnName(int index)
        {
            if(!columns.isEmpty())
            {
                return columns.get(index).toString();
            }
            else
            {
                switch(index)
                {
                    case 0:
                        return "Start Time";
                    case 1:
                        return "Duration";
                    case 2:
                        return "Sound Type";
                       
                }
                return "";
            }
        }





Now my table is in another form/Class. I Initialised The vectors "data" and "columns" by calling the above class. and then passed these vectors to DefaultTableModel.

My code in Table Form is

 
             TimeTable = new javax.swing.JTable();
             InsertFileDataToJTable model = new InsertFileDataToJTable();
             TimeTable.setModel(new DefaultTableModel(model.data,model.columns));

             jScrollPane2.setViewportView(TimeTable);




I m using netbeans IDE So Most of the code is autogenerated.

When i ran the application with null data i.e when File to be read only contains column names and no data. every thing is fine it works, adds data enterd by user to jtable and then saves it to file when user clicks SAVE button but when application is ran again i.e when file contains data previously saved by user then it throws CLASSCASTEXCEPTION as shown below.

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:268)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:224)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:159)
at bin.Main_window.initComponents(Main_window.java:183)
at bin.Main_window.<init>(Main_window.java:40)
at bin.Main_Window$16.run(Main_window.java:517)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)


Please Help me I am stuck at it from one week. :boat:

Thanks for any help.........

Is This A Good Question/Topic? 0
  • +

Replies To: Problem Importing data from file to Jtable

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem Importing data from file to Jtable

Posted 20 June 2012 - 08:33 PM

View Postankurtyagi, on 20 June 2012 - 11:28 PM, said:

i m trying to develop a application which stores data entered by user in Jtable

I'll stop you right away. You missed the concept.

You do not store data in a JTable
A Jtable is just a fancy GUI element that displays, in a nice way, the values contained in its model.
Change the Model, the values displayed will change.

If your JTable dioes not have a Model, here is a tutorial I wrote on how to convert it

http://www.dreaminco...h-a-tablemodel/
Was This Post Helpful? 1
  • +
  • -

#3 ankurtyagi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-June 12

Re: Problem Importing data from file to Jtable

Posted 22 June 2012 - 12:52 AM

View Postpbl, on 20 June 2012 - 08:33 PM, said:

View Postankurtyagi, on 20 June 2012 - 11:28 PM, said:

i m trying to develop a application which stores data entered by user in Jtable

I'll stop you right away. You missed the concept.

You do not store data in a JTable
A Jtable is just a fancy GUI element that displays, in a nice way, the values contained in its model.
Change the Model, the values displayed will change.

If your JTable dioes not have a Model, here is a tutorial I wrote on how to convert it

http://www.dreaminco...h-a-tablemodel/



Sorry Sir I explained it to you in wrong way.......
I know that data is stored in tablemodel and Jtable only displays it.....

I meant to say that how can i read data from file into tablemodel and display it in table... +(plus) I also want to change that table model like deleting entries, adding new entries, editing them etc.........

I hope you get it now.....

Thanks for the hyperlink of tutorial but it donot contains what i want.....

Basically requirement is to read data from a file into tablemodel........ And i am able to do so but i am getting an error of Type "CLASSCASTEXCETION". See above i have added the full list of error that is being thrown.....

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Problem Importing data from file to Jtable

Posted 22 June 2012 - 02:06 AM

Quote

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



I'm not sure if the above is the reason for the error but it's wrong - don't divide by the column count (row count == data.size)

You'd be better off just making your own DefaultTableModel and setting it on the table. See

http://technojeeves....v-to-tablemodel

( Change to split("\t+") )
Was This Post Helpful? 3
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10391
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Problem Importing data from file to Jtable

Posted 22 June 2012 - 09:06 AM

Also, Java Collections like Vectors are generic. If you can't specify the type more specifically than Object, use Vector<Object> to avoid a deprecation warning.
Was This Post Helpful? 0
  • +
  • -

#6 ankurtyagi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-June 12

Re: Problem Importing data from file to Jtable

Posted 23 June 2012 - 06:40 AM

View Postg00se, on 22 June 2012 - 02:06 AM, said:

Quote

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



I'm not sure if the above is the reason for the error but it's wrong - don't divide by the column count (row count == data.size)

You'd be better off just making your own DefaultTableModel and setting it on the table. See

http://technojeeves....v-to-tablemodel

( Change to split("\t+") )




ThankYou Thankyou Thankyou Thankyou Thankyou Thankyou So Much......
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1