I have simple program where I want load data from CSV and add to JTable. Button Load Data is for launch the loading sequence and get data to table. Loading to table works fine but refreshing table doesn't work. I try revalidate and repaint but this doesn't work. Debugging of program revealed that data are insert in JTable object but are not displayed.
This is my App2.java class
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();
}
}
This is Data.class but this work fine.
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
}
And this is my main class
package com.App;
public class Main {
public static void main(String[] args) {
App2 app2 =new App2();
app2.initFrame();
System.out.println("OK");
}
}