1

I looking for way to put data from CSV file to table. I have problem with refresh rate. Table print out just one row. How do I change this code to print more rows in table? Because in CSV file I have for example 20 or more rows.

import java.awt.Component;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class FileParser {
    String aFileToRead = "";

    public FileParser(String aNameOfFile) {
        this.aFileToRead = aNameOfFile;
    }

    public void readFileToObjects() {
        int comma1=0;
        int comma2=0;
        int comma3=0;
        int comma4=0;
        int comma5=0;
        int comma6=0;
        int comma7=0;
        int comma8=0;
        int comma9=0;
        int comma10=0;
        int comma11=0;
        int comma12=0;
        int comma13=0;
        int comma14=0;
        int comma15=0;
        int endOfLine=0;
        double aTemperature = 0.0;
        double aHumidity = 0.0;
        double aPressure = 0.0;
        double aOrientation_x = 0.0;
        double aOrientation_y = 0.0;
        double aOrientation_z = 0.0;
        double aMagnetic_x = 0.0;
        double aMagnetic_y = 0.0;
        double aMagnetic_z = 0.0;
        double aSpeed_x = 0.0;
        double aSpeed_y = 0.0;
        double aSpeed_z = 0.0;
        String aGyroscope_x = "";
        String aGyroscope_y = "";
        String aGyroscope_z = "";
        String aTime = "";
        String chartPositionAsString="";
        JFrame frame = new JFrame();


        try {
            String aStr = null;
            FileInputStream aFile = new FileInputStream(this.aFileToRead);
            InputStreamReader aInReader = new InputStreamReader(aFile);
            BufferedReader theBufReader = new BufferedReader(aInReader);
            while ((aStr = theBufReader.readLine()) != null) {
                comma1 = aStr.indexOf(",");
                chartPositionAsString = aStr.substring(0, comma1);
                aTemperature = Double.parseDouble(chartPositionAsString);

                comma2 = aStr.indexOf(",", comma1 + 1);
                chartPositionAsString = aStr.substring(comma1 + 1, comma2);
                aHumidity = Double.parseDouble(chartPositionAsString);

                comma3 = aStr.indexOf(",", comma2 + 1);
                chartPositionAsString = aStr.substring(comma2 + 1, comma3);
                aPressure = Double.parseDouble(chartPositionAsString);

                comma4 = aStr.indexOf(",", comma3 + 1);
                chartPositionAsString = aStr.substring(comma3 + 1, comma4);
                aOrientation_x = Double.parseDouble(chartPositionAsString);

                comma5 = aStr.indexOf(",", comma4 + 1);
                chartPositionAsString = aStr.substring(comma4 + 1, comma5);
                aOrientation_y = Double.parseDouble(chartPositionAsString);

                comma6 = aStr.indexOf(",", comma5 + 1);
                chartPositionAsString = aStr.substring(comma5 + 1, comma6);
                aOrientation_z = Double.parseDouble(chartPositionAsString);

                comma7 = aStr.indexOf(",", comma6 + 1);
                chartPositionAsString = aStr.substring(comma6 + 1, comma7);
                aMagnetic_x = Double.parseDouble(chartPositionAsString);

                comma8 = aStr.indexOf(",", comma7 + 1);
                chartPositionAsString = aStr.substring(comma7 + 1, comma8);
                aMagnetic_y = Double.parseDouble(chartPositionAsString);

                comma9 = aStr.indexOf(",", comma8 + 1);
                chartPositionAsString = aStr.substring(comma8 + 1, comma9);
                aMagnetic_z = Double.parseDouble(chartPositionAsString);

                comma10 = aStr.indexOf(",", comma9 + 1);
                chartPositionAsString = aStr.substring(comma9 + 1, comma10);
                aSpeed_x = Double.parseDouble(chartPositionAsString);

                comma11 = aStr.indexOf(",", comma10 + 1);
                chartPositionAsString = aStr.substring(comma10 + 1, comma11);
                aSpeed_y = Double.parseDouble(chartPositionAsString);

                comma12 = aStr.indexOf(",", comma11 + 1);
                chartPositionAsString = aStr.substring(comma11 + 1, comma12);
                aSpeed_z = Double.parseDouble(chartPositionAsString);

                comma13 = aStr.indexOf(",", comma12 + 1);
                chartPositionAsString = aStr.substring(comma12 + 1, comma13);
                aGyroscope_x = (chartPositionAsString);

                comma14 = aStr.indexOf(",", comma13 + 1);
                chartPositionAsString = aStr.substring(comma13 + 1, comma14);
                aGyroscope_y = (chartPositionAsString);

                comma15 = aStr.indexOf(",", comma14 + 1);
                chartPositionAsString = aStr.substring(comma14 + 1, comma15);
                aGyroscope_z = (chartPositionAsString);

                endOfLine = aStr.length();
                aTime = chartPositionAsString = aStr.substring(comma15 + 1, endOfLine);

               Env aEnv = new Env(aTemperature, aHumidity, aPressure, 
                                  aOrientation_x, aOrientation_y, aOrientation_z, 
                                  aMagnetic_x, aMagnetic_y, aMagnetic_z, aSpeed_x, aSpeed_y, aSpeed_z, 
                                  aGyroscope_x, aGyroscope_y, aGyroscope_z,
                                  aTime);

               String columnNames1[] = { "Temperature", "Humidity", "Pressure",
                    "Orientation yaw", "Orientation pitch", "Orientation roll", 
                       "Magnetic X", "Magnetic Y", "Magnetic Z",
                       "Speed X", "Speed Y", "Speed Z",
                       "Gyroscope X", "Gyroscope Y", "Gyroscope Z",
                       "Time" };    

                Object[][] rowData1 = {
                          {aEnv.getTemperature(), aEnv.getHumidity(), aEnv.getPressure(),
                           aEnv.getOrientation_x(), aEnv.getOrientation_y(), aEnv.getOrientation_z(), 
                           aEnv.getMagnetic_x(), aEnv.getMagnetic_y(), aEnv.getMagnetic_z(), 
                           aEnv.getSpeed_x(), aEnv.getSpeed_y(), aEnv.getSpeed_z(), 
                           aEnv.getGyroscope_x(), aEnv.getGyroscope_y(), aEnv.getGyroscope_z(),
                           aEnv.getTime(),}};

                JTable table = new JTable(rowData1, columnNames1);
                JScrollPane scrollPane = new JScrollPane(table);
                table.getTableHeader().setFont(new Font("Serif", Font.BOLD, 15));

                frame.add((Component) scrollPane, "Center");  

                frame.setTitle("Enviromental Data");
                frame.setDefaultCloseOperation(3);
                frame.setSize(1900, 100);
                frame.setVisible(true);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("The File is Not in The Correct Directory");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
7
  • 4
    rather than re-invent the wheel try using something like csvreader Commented Nov 4, 2016 at 0:38
  • 4
    Your code looks very repetitive. Perhaps you want an array and a loop? Commented Nov 4, 2016 at 0:39
  • 1
    BTW you have the creation of your JTable within your while loop - meaning that it will get re-created for each row Commented Nov 4, 2016 at 0:40
  • Yes I was thinking to put comma or chartPositionAsString to arrey. Commented Nov 4, 2016 at 0:49
  • I want to simplify this also. But my mayor problem is to print out in table So its get refresh when new data go to CSV. Cos this run on Raspberry Pi. Python get data to csv from Sense Hat and Java display this in table. Commented Nov 4, 2016 at 0:53

1 Answer 1

2

You are using a lot of variable for nothing.

List<String[]> rows = new ArrayList<>();
while ((aStr = theBufReader.readLine()) != null) {
    String[] dataLine = aStr.split(",");
    rows.add(dataLine);
}

Then, create the JTable, you probably going to need to recreate the Object[][] from the list. Or see this to Populate JTable Using List

If you really need to convert some row to number (for formatting), you can use a loop to convert those columns, using an array containing the column index and loop only on those.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.