Skip to content

Commit 288726c

Browse files
authored
Adding source files of Plotter
# having lots of bugs, kindly help fix it.
1 parent d0e6d91 commit 288726c

12 files changed

+1638
-0
lines changed

CSVextract.java

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import java.io.File;
2+
import java.io.FileNotFoundException;
3+
import java.io.FileReader;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
import java.util.regex.Pattern;
7+
8+
import com.univocity.parsers.common.processor.BeanListProcessor;
9+
import com.univocity.parsers.csv.CsvParser;
10+
import com.univocity.parsers.csv.CsvParserSettings;
11+
12+
public class CSVextract {
13+
public int row,column,datarow=row-1,datacolumn = column-1;
14+
public String[][] entry;
15+
public CSVextract(File file)
16+
{
17+
try {
18+
readCSV(file);
19+
} catch (FileNotFoundException e) {
20+
// TODO Auto-generated catch block
21+
e.printStackTrace();
22+
}
23+
}
24+
public String[][] getEntry()
25+
{
26+
return entry;
27+
}
28+
public double[][] getDataTable()
29+
{
30+
double dataTable[][] = new double[row][column];
31+
for(int i=1;i<row;i++)
32+
{
33+
for(int j=1;j<column;j++)
34+
{
35+
dataTable[i][j] = Double.parseDouble(entry[i][j]);
36+
}
37+
}
38+
return dataTable;
39+
}
40+
41+
public String[] getFields()
42+
{
43+
String field[] = new String[column-1];
44+
for(int i=1;i<row;i++)
45+
{
46+
field[i]= entry[0][i];
47+
}
48+
return field;
49+
}
50+
public String[] getKey()
51+
{
52+
String[] key = new String[row-1];
53+
for(int i=1;i<row;i++)
54+
{
55+
key[i] = entry[i][0];
56+
}
57+
return key;
58+
}
59+
public String[][] readCSV (File file) throws FileNotFoundException
60+
{
61+
String entry[][] = new String[100][100];
62+
row=0;column=0;int columnCount=0;
63+
/*Scanner reader;
64+
try
65+
{
66+
reader = new Scanner(file);
67+
Scanner sc2 = new Scanner(file);
68+
while (sc2.hasNextLine())
69+
{
70+
for (String item : sc2.next().split(","))
71+
{
72+
entry[row][column]=reader.next();
73+
column++;
74+
System.out.println(item);System.out.println(column+" "+row);
75+
}
76+
}*/
77+
/////
78+
/*while(reader.hasNext())
79+
{
80+
reader.useDelimiter(",|\\r\\n");
81+
while(reader.hasNext())
82+
{
83+
try{
84+
85+
if(Pattern.matches(Pattern.compile("\\r\\n"),reader.next()))
86+
{
87+
row++;System.out.println("br");
88+
}
89+
System.out.println(column+" "+row);
90+
entry[row][column]=reader.next();
91+
column++;
92+
93+
}catch(NumberFormatException e) {}
94+
}
95+
}
96+
column ++;
97+
}
98+
catch (FileNotFoundException e) {
99+
e.printStackTrace();
100+
}
101+
for(int i=0;i<row;i++)
102+
{
103+
for(int j=0;j<column;j++)
104+
{
105+
//System.out.print(entry[i][j]+" ");
106+
}
107+
System.out.println();
108+
}*/
109+
110+
// 1st, config the CSV reader
111+
CsvParserSettings settings = new CsvParserSettings();
112+
settings.getFormat().setLineSeparator("\n");
113+
settings.selectFields("Color1", "Color3", "Color2");
114+
// 2nd, creates a CSV parser with the configs
115+
CsvParser parser = new CsvParser(settings);
116+
// 3rd, parses all rows of data in selected columns from the CSV file into a matrix
117+
List<String[]> resolvedData = parser.parseAll(new FileReader(file));
118+
// 3rd, process the matrix with business logic
119+
for (String[] row : resolvedData)
120+
{
121+
StringBuilder strBuilder = new StringBuilder();
122+
for (String col : row)
123+
{
124+
strBuilder.append(col).append("\t");
125+
}
126+
System.out.println(strBuilder);
127+
}
128+
129+
return entry;
130+
}
131+
132+
}

CSVfolder.java

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package graph;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Color;
5+
import java.awt.EventQueue;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
import java.io.File;
9+
10+
import javax.swing.JFileChooser;
11+
import javax.swing.JFrame;
12+
import javax.swing.JLabel;
13+
import javax.swing.JMenuItem;
14+
import javax.swing.JOptionPane;
15+
import javax.swing.JPanel;
16+
import javax.swing.JTextField;
17+
import javax.swing.border.EmptyBorder;
18+
import javax.swing.filechooser.FileFilter;
19+
import javax.swing.filechooser.FileNameExtensionFilter;
20+
import java.awt.Font;
21+
22+
public class CSVfolder extends JFrame {
23+
24+
public static JPanel contentPane;
25+
public String loc;
26+
JMenuItem open=new JMenuItem("Open File");
27+
private JTextField textloc;
28+
public static File file;
29+
30+
/**
31+
* Launch the application.
32+
*/
33+
public static void main(String[] args) {
34+
EventQueue.invokeLater(new Runnable() {
35+
public void run() {
36+
try {
37+
CSVfolder frame = new CSVfolder();
38+
frame.setVisible(true);
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
});
44+
}
45+
46+
47+
public File getFile()
48+
{
49+
return file;
50+
}
51+
public CSVfolder() {
52+
setTitle("Select CSV File to Import");
53+
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
54+
setBounds(100, 100, 450, 300);
55+
contentPane = new JPanel();
56+
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
57+
setContentPane(contentPane);
58+
contentPane.setLayout(null);
59+
60+
61+
textloc = new JTextField();
62+
textloc.setFont(new Font("Tahoma", Font.PLAIN, 13));
63+
textloc.setToolTipText("Selected File");
64+
textloc.setBackground(new Color(255, 248, 220));
65+
textloc.setBounds(30, 5, 637, 35);
66+
contentPane.add(textloc);
67+
textloc.setColumns(10);
68+
69+
JFileChooser fc = new JFileChooser("D:\\GraphPlottingSuite\\DataCSV\\");
70+
fc.setFileFilter(new FileNameExtensionFilter(".csv","csv"));
71+
fc.setApproveButtonToolTipText("Click here select FILE");
72+
fc.setAcceptAllFileFilterUsed(false);
73+
fc.setApproveButtonText("IMPORT");
74+
fc.addActionListener(new ActionListener() {
75+
public void actionPerformed(ActionEvent e) {
76+
77+
int i=JFileChooser.APPROVE_OPTION;
78+
if(i==JFileChooser.APPROVE_OPTION){
79+
try
80+
{
81+
file = fc.getSelectedFile();
82+
loc =file.getPath();
83+
textloc.setText(loc);
84+
TableView.txtImport.setText(loc);
85+
if(TableView.selected)
86+
TableView.tablelabel.setText(loc);
87+
JOptionPane.showMessageDialog(null, "Selected");
88+
dispose();
89+
}
90+
catch (Exception ex) {ex.printStackTrace(); }
91+
}
92+
}
93+
});
94+
fc.setBounds(30, 51, 637, 333);
95+
96+
contentPane.add(fc);
97+
98+
JLabel lblNewLabel_1 = new JLabel("File Selected");
99+
lblNewLabel_1.setBounds(5, 5, 708, 466);
100+
contentPane.add(lblNewLabel_1);
101+
102+
setVisible(true);
103+
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
104+
setBounds(100, 100, 734, 515);
105+
}
106+
107+
}

CSVreader.java

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import java.awt.BorderLayout;
2+
import java.awt.EventQueue;
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.Scanner;
6+
7+
import javax.swing.JFrame;
8+
import javax.swing.JPanel;
9+
import javax.swing.border.EmptyBorder;
10+
import javax.swing.filechooser.FileNameExtensionFilter;
11+
import javax.swing.JFileChooser;
12+
import javax.swing.JPasswordField;
13+
import javax.swing.JScrollPane;
14+
15+
import java.awt.event.ActionListener;
16+
import java.awt.event.ActionEvent;
17+
import javax.swing.JTable;
18+
19+
public class CSVreader extends JFrame {
20+
21+
private JPanel contentPane;
22+
//public File file;
23+
private JTable table;
24+
public static void main(String[] args) {
25+
EventQueue.invokeLater(new Runnable() {
26+
public void run() {
27+
try {
28+
CSVreader frame = new CSVreader();
29+
frame.setVisible(true);
30+
} catch (Exception e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
});
35+
}
36+
37+
public void drawTable(File file)
38+
{
39+
40+
JScrollPane scrollPane = new JScrollPane(table);
41+
scrollPane.setBounds(10, 11, 706, 430);
42+
contentPane.add(scrollPane);
43+
44+
CSVextract extract = new CSVextract(file);
45+
try {
46+
extract.readCSV(file);
47+
} catch (FileNotFoundException e) {
48+
// TODO Auto-generated catch block
49+
e.printStackTrace();
50+
}
51+
table = new JTable();
52+
table.setBounds(810, 60, 31, 49);
53+
scrollPane.add(table);
54+
55+
}
56+
public CSVreader()
57+
{
58+
File file;
59+
setVisible(true);
60+
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
61+
setBounds(100, 100, 854, 555);
62+
contentPane = new JPanel();
63+
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
64+
setContentPane(contentPane);
65+
contentPane.setLayout(null);
66+
67+
JFileChooser fc = new JFileChooser("D:\\GraphPlottingSuite\\DataCSV\\");
68+
fc.setFileFilter(new FileNameExtensionFilter("*.csv",".csv","csv","CSV",".CSV"));
69+
int result = fc.showOpenDialog(this);
70+
if (result == JFileChooser.OPEN_DIALOG) {
71+
file = fc.getSelectedFile();
72+
drawTable(file);
73+
} else if (result == JFileChooser.CANCEL_OPTION) {
74+
System.out.println("Cancel was selected");
75+
}
76+
fc.setBounds(0, 0, 761, 286);
77+
//contentPane.add(fc);
78+
//////////////
79+
}
80+
}

0 commit comments

Comments
 (0)