我有简单的程序,我希望从CSV加载数据并添加到JTable。按钮加载数据用于启动加载序列并将数据获取到表。加载到表工作正常但刷新表不起作用。我尝试重新验证并重新绘制,但这不起作用。调试程序显示数据是插入JTable对象但不显示。
这是我的App2.java类
package com.App;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Arrays;
public class App2 extends Data {
private JTable tableview;
private JButton deleteAllSpacesButton;
private JButton checkPSCButton;
private JButton insertRowButton;
private JButton loadDataButton;
private JProgressBar progressBar1;
private JButton button5;
private JPanel panelMain;
private JScrollPane scrollingwindow;
private JFrame frame;
//top panel
private JMenuBar menuBar;
private JMenu fileMenu=new JMenu("File");
private JMenu editMenu=new JMenu("Edit");
private JMenu helpMenu=new JMenu("Help");
private JMenuItem [] fileMenuItems={new JMenuItem("New"),new JMenuItem("Open"),new JMenuItem("Save"),new JMenuItem("Exit")};
private JMenuItem [] editMenuItems={new JMenuItem("Find"),new JMenuItem("Undo"),new JMenuItem("Redo")};
private JMenuItem [] helpMenuItems={new JMenuItem("Help-Info")};
public App2() {
Data data=new Data();
checkPSCButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
loadDataButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pressLoadDataButton();
}
});
insertRowButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
deleteAllSpacesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
public void initFrame(){
loadToJMenu(fileMenu,fileMenuItems);
loadToJMenu(editMenu,editMenuItems);
loadToJMenu(helpMenu,helpMenuItems);
loadToMenuBar();
frame = new JFrame("App2");
frame.setLayout(new FlowLayout(FlowLayout.LEADING));
//frame.add(panel2);
frame.setJMenuBar(menuBar);
frame.add(new App2().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void loadToMenuBar(){
menuBar=new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
}
private void loadToJMenu(JMenu menu,JMenuItem [] items){
for (JMenuItem item : items) {
menu.add(item);
System.out.println(item);
}
}
private void pressLoadDataButton(){
loadData();
tableview=new JTable(rowData,namesOfColumns);
System.out.println(Arrays.toString(getNamesOfColumns()));
System.out.println("load");
panelMain.repaint();
panelMain.revalidate();
}
}
这是Data.class,但这很好。
package com.App;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Data {
public Object[] namesOfColumns;
public Object[][] rowData;
public Object[] rowPSC;
private String csvfile="data.csv";
public Data() {
}
public void loadData(){
loadNamesFromCSV();
loadDataFromCSV();
}
public Object[] getNamesOfColumns() {
return namesOfColumns;
}
public Object[] getRowPSC() {
return rowPSC;
}
public void setRowPSC(Object[] rowPSC) {
this.rowPSC = rowPSC;
}
public Object[][] getRowData() {
return rowData;
}
public void setRowData(Object[][] rowData) {
this.rowData = rowData;
}
public void setNamesOfColumns(Object[] namesOfColumns) {
this.namesOfColumns = namesOfColumns;
}
private void loadNamesFromCSV(){
BufferedReader br;
String line;
String splitby=";";
try {
br=new BufferedReader(new FileReader(csvfile));
line=br.readLine();
namesOfColumns= line.split(splitby);
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadDataFromCSV(){
BufferedReader br;
String line;
String splitby=";";
List<String[]> tmpList=new ArrayList<String[]>();
try {
br=new BufferedReader(new FileReader(csvfile));
while((line=br.readLine())!=null){
String []arrayline=line.split(splitby);
tmpList.add(arrayline);
}
} catch (java.io.IOException e) {
e.printStackTrace();
}
tmpList.remove(0);
rowData=convertListToArray(tmpList);
}
private String[][] convertListToArray(List<String[]> list){
String[][] array=new String[list.size()][(list.get(0)).length];
for(int i=0;i<list.size();i++){
String[] row=list.get(i);
array[i]=row;
}
return array;
}
//end of class
}
这是我的主要课程
package com.App;
public class Main {
public static void main(String[] args) {
App2 app2 =new App2();
app2.initFrame();
System.out.println("OK");
}
}