views:

220

answers:

1

I have been attempting to load CSV data into a java applet. I have been able to load the data into a java program but when I make the attempt to recreate this into a java applet, i am having difficulties. Here is the code I have that will run but display nothing:

import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.applet.*;

    public class test3 extends java.applet.Applet
    {

        public void init() {

            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                initComponents();
                }
                });
            } catch (Exception ex) {
            ex.printStackTrace();
            }
        }


    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable JTable1;
    private DefaultTableModel model;

    private void initComponents()
    {
        String aLine ;
        Vector columnNames = new Vector();
        Vector data = new Vector();

        try
        {
            FilePermission p = new FilePermission("DL0215103A.csv", "read" );
            FileInputStream File = new FileInputStream("DL0215103A.csv");
            BufferedReader br = new BufferedReader(new InputStreamReader(File));

            // 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, ",");
                Vector row = new Vector();

                while(st2.hasMoreTokens())
                {
                    row.addElement(st2.nextToken());
                }
                data.addElement( row );
            }
            br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        //  Create table using the DefaultTableModel

        model = new DefaultTableModel(data, columnNames);
        JTable1 = new JTable(model);

        jScrollPane1.setViewportView(JTable1);
        add(jScrollPane1, java.awt.BorderLayout.CENTER);


    }

}
+1  A: 

Maybe your problem is related to actually finding the file, when an applet is launched as an applet different criteria are used to find and load relative files, check java tutorial about this topic: link.

Jack
thanks for the advice. I made the below changes but nothing changed:File file1 = new File("DL0215103A.csv");FileInputStream File2 = new FileInputStream(file1);BufferedReader br = new BufferedReader(new InputStreamReader(File2));
eric