CSV ファイルからデータを読み取って JTable に表示しようとしていますが、いくつか問題があります。私は初心者ですので、ご容赦ください。いくつかのソースからサンプルコードを見て組み込みましたが、役に立ちませんでした。表は表示されますが、空白です。印刷できるので、データを読んでいることがわかります。ModelTable のセットアップに何か問題があると思われます。どんな助けでも大歓迎です。
package t1data;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
import javax.swing.table.*;
public class T1Data extends JPanel implements ActionListener {
public T1Data() {
super(new BorderLayout(3,3));
JTable Table = new JTable(new MyModel());
Table.setPreferredScrollableViewportSize(new Dimension(700, 70));
Table.setFillsViewportHeight(true);
JPanel ButtonOpen = new JPanel( new FlowLayout(FlowLayout.CENTER) );
JButton OpenFile = new JButton("Open");
JButton SaveFile = new JButton("Save");
ButtonOpen.add(OpenFile);
add(ButtonOpen, BorderLayout.SOUTH);
OpenFile.addActionListener(this);
SaveFile.addActionListener(this);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(Table);
//Add the scroll pane to this panel.
add(scrollPane, BorderLayout.CENTER);
// add a nice border
setBorder(new EmptyBorder(5,5,5,5));
}
public void actionPerformed (ActionEvent Event) {
JFileChooser fc = new JFileChooser();
boolean pressed = true;
// if (Event.getSource() == OpenFile) {
if (pressed) {
int ReturnVal = fc.showOpenDialog(null);
// if (ReturnVal == JFileChooser.APPROVE_OPTION) {
CSVFile Rd = new CSVFile();
//ArrayList<String[]> Rs2 = new ArrayList<>();
MyModel NewModel = new MyModel();
File DataFile = fc.getSelectedFile();
ArrayList<String[]> Rs2 = Rd.ReadCSVfile(DataFile);
NewModel.AddCSVData(Rs2);
System.out.println ("Rows: " +NewModel.getRowCount());
System.out.println ("Cols: " +NewModel.getColumnCount());
}
}
// Method for reading CSV file
public class CSVFile {
private ArrayList<String[]> Rs = new ArrayList<>();
private String[] OneRow;
public ArrayList<String[]> ReadCSVfile (File DataFile) {
try {
BufferedReader brd = new BufferedReader (new FileReader(DataFile));
while (brd.readLine() != null) {
String st = brd.readLine();
OneRow = st.split(",|\\s|;");
Rs.add(OneRow);
System.out.println (Arrays.toString(OneRow));
} // end of while
} // end of try
catch (Exception e) {
String errmsg = e.getMessage();
System.out.println ("File not found:" +errmsg);
} // end of Catch
return Rs;
}// end of ReadFile method
}// end of CSVFile class
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("T1Data");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
T1Data newContentPane = new T1Data();
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
class MyModel extends AbstractTableModel {
private String[] columnNames = { "1", "2", "3", "4", "5", "6"};
private ArrayList<String[]> Data = new ArrayList<>();
public void AddCSVData(ArrayList<String[]> DataIn) {
this.Data = DataIn;
this.fireTableDataChanged();
}
@Override
public int getColumnCount() {
return columnNames.length;//length;
}
@Override
public int getRowCount() {
return Data.size();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col)
{
return Data.get(row)[col];
}
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
// T1Data.createAndShowGUI();
}
}