1

I want to read the values of csv file and display that in AWT table when start button is pressed. Pressing stop button should stop reading the values. Reset button must be used to clear the table. I am new to AWT. Here is what i have tried.

package AWTTable;

    import java.awt.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.FileReader;

    /**
     *
     * @author Admin
     */
    public class AWTTableUpdate {

        public static void main(String[] args) {
            Frame f = new Frame();
            Label label = new Label("Date and Time ");
            Label label1 = new Label(" Name");
            Label label2 = new Label("contents");
            final Vector columnNames = new Vector();
            final Vector data = new Vector();
            final TextField text = new TextField(20);
            Button b = new Button("Start");
            Button b1 = new Button("Stop");
            Button b2 = new Button("Reset");
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {


                    try {

                        String strFile = "c:/companylist.csv";  

                        BufferedReader br = new BufferedReader(new FileReader(strFile));
                        String strLine = "";
                        StringTokenizer st = null;
                        int lineNumber = 0, tokenNumber = 0;

                        ArrayList<String> arrayList = new ArrayList<String>();

                        while ((strLine = br.readLine()) != null) {
                            lineNumber++;

                            st = new StringTokenizer(strLine, ",");
                            while (st.hasMoreTokens()) {

                                tokenNumber++;
                                arrayList.add(st.nextToken());

                            tokenNumber = 0;
                        }
                       Object[] elements = arrayList.toArray();

                        Scanner input = new Scanner(System.in);
                        System.out.print("Enter Ticker symbol");

                    } }catch (Exception e1) {
                        System.out.println("Exception while reading csv file: " + e1);
                    }

                    JTable table = new JTable(data, columnNames);
                    JScrollPane scrollPane = new JScrollPane(table);
                    JFrame frame = new JFrame();
                    frame.add(scrollPane);
                    frame.setVisible(true);
                    frame.pack();
                    frame.repaint();
                }
            });
            Panel p = new Panel(new GridLayout(6, 6));
            p.add(label);
            p.add(label1);
            p.add(label2);
         //   p.add(text);
            p.add(b);
            p.add(b1);
            p.add(b2);
            f.add(p);
            f.setVisible(true);
            f.pack();
        }
    }

What I get is not in the form of table. After clicking start button I get blank window. But I want to display the data. Thank you

Sirko
  • 72,589
  • 19
  • 149
  • 183
sahana
  • 43
  • 1
  • 2
  • 7

1 Answers1

1

Your data vector is initialised but never filled in with data, you should have an "add" statement after obtaining the array of elements:

Object[] elements = arrayList.toArray();
data.add(elements);

Also move those statements to the end of the outer loop, so you can add to your data vector rows of data, instead of a single row with all the fields of every row in a single one.

Additionally, you may be interested in linking one of your buttons with a "data.clear()" statement.


By the way, I see a kind of weird logic inside your inner loop when counting the number of tokens:

while (st.hasMoreTokens()) {

    tokenNumber++;
    arrayList.add(st.nextToken());

    tokenNumber = 0;
}

Your last statement inside that loop should be outside of it, otherwise your "tokenNumber" variable will always hold a 0 or 1 value.

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
  • And you are not providing any column names. And why do you need the call to scanner in that while loop? And it's a bit strange to mix awt with swing in that way. Structuring your code into several small methods could help making your intent clear by the way. – nansen Mar 25 '12 at 13:05
  • you are right, he is mixing Swing and AWT but his code written with only Swing components will not show anything as he is initialising the table with an empty vector. I'm just trying to solve his problem without the need to rewrite everything. – Alonso Dominguez Mar 25 '12 at 13:17