0

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 form of GUI

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");
    }
}
  • 1
    Instead of doing `tableview=new JTable(rowData,namesOfColumns);`, which creates a new instance of `JTable` which is not been added to your view in anyway, simply create a new `TableModel` and apply it to the pre-existing `JTable` via the `setModel` method. *"How do I do that?*" - You could start by reading [How to use tables](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – MadProgrammer Feb 20 '18 at 23:14
  • Ok, I implemented Table Model like you tell me, I am setting Model in the buttonactionlistener and calling method that included function model.fireDataTableChanged and it still don't work. I debugged program and Table Model is correctly added to a Table. Can you help me? – tino_kranec Feb 21 '18 at 07:53
  • You never need to call `model.fireDataTableChanged` externally of the model itself – MadProgrammer Feb 21 '18 at 08:14
  • Yes I call it internally in the model[update] I handle it, thanks, i have last problem with names because it show A,B,C,D instead of names – tino_kranec Feb 21 '18 at 08:15
  • `Data` seems to be virtual a `TableModel`. It'd consider extending it from `AbstractTableModel` and implement the required functionality. It would generally make your life simpler – MadProgrammer Feb 21 '18 at 09:58
  • [That's one example of CSV to `JTable`](https://stackoverflow.com/questions/10466079/import-csv-to-jtable) – MadProgrammer Feb 21 '18 at 09:59

0 Answers0