У меня есть файл CSV (5 столбцов, с | в качестве разделителя), и я хочу заполнить 2 JTable
столбцы двумя столбцами данных из файла CSV.
public class jTable extends JFrame {
public static int rowNumber() {
int num = 0;
try {
File files = new File("C:\\BorsaItalia2.csv");
BufferedReader bf = new BufferedReader(new FileReader(files));
String lines = "";
while ((lines = bf.readLine()) != null) {
num++;
}
} catch (Exception e) {
e.printStackTrace();
}
return num;
}
JTable table;
public jTable() {
setLayout(new FlowLayout());
String[] columnNames = {"a", "b", "c", "d", "e"};
Object[][] data = new Object[rowNumber()][5];
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(600, 950));
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
add(scroll);
}
public static void main(String[] args) {
try {
int row = 0;
int col = 0;
Object[][] imported = new Object[rowNumber()][5];
File file = new File("C:\\BorsaItalia2.csv");
BufferedReader bfr = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bfr.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "|");
col = 0;
while (st.hasMoreTokens()) {
imported[row][col] = st.nextToken();
System.out.println("number["+ row + "]["
+ col + "]:" + imported[row][col]);
col++;
}
row++;
}
bfr.close();
Object[] description = new Object[imported.length];
Object[] symbol = new Object[imported.length];
for (int i = 0; i < imported.length; i++) {
description[i] = imported[i][2];
symbol[i] = imported[i][0];
}
for (int i = 1; i < imported.length; i++) {
System.out.println("Description is " + description[i]
+ " and symbol is: " + symbol[i]);
}
System.out.println("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("error " + e);
}
jTable gui = new jTable();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(650, 950);
gui.setVisible(true);
gui.setTitle("Project Table");
}
}
Я хотел бы заполнить столбец таблицы a
с Object[] symbol
и столбец c
с Object[] description
.
Любая помощь в кодировании действительно приветствуется.
Спасибо всем.