0

I am trying to get My Application to display the data from a CSV IN the JTable and not output it in Netbeans > output. I hope someone can be able to help me with the solution. Here is the source code and results of what I have already.

I could not see what I did wrong, the code I posted below should already display the data in the table but it does not display in as it should. I was hoping someone here can help me see: where did I go wrong?

Source Code:

public class Asist_payment_import_v1 extends JPanel implements ActionListener{
private final JTable Table;

public Asist_payment_import_v1(){
    super(new BorderLayout(3,3));


    this.Table = new JTable(new MiModel());
    this.Table.setPreferredScrollableViewportSize(new Dimension(700,70));
    this.Table.setFillsViewportHeight(true);

    JPanel ButtonOpen = new JPanel(new FlowLayout(FlowLayout.CENTER));

    JButton open_file = new JButton("Open");
    JButton save_file = new JButton("Save");

    ButtonOpen.add(open_file);
    add(ButtonOpen, BorderLayout.SOUTH);

    open_file.addActionListener(this);
    save_file.addActionListener(this);

    //create scrollPane to this Panel
    JScrollPane scrollpane = new JScrollPane(Table);

    //add the scroll pane to this pane
    add(scrollpane,BorderLayout.CENTER);

    //add Border
    setBorder(new EmptyBorder(5,5,5,5));

}

public void actionPerformed(ActionEvent Event)
{

    JFileChooser file_chooser = new JFileChooser();
    boolean pressed = true;

    if(pressed)
    {
        int ReturnVal = file_chooser.showOpenDialog(null);
        CSVFile Rd = new CSVFile();

        MiModel NewModel = new MiModel();
        this.Table.setModel(NewModel);// to set the Table model

        File DataFile = file_chooser.getSelectedFile();
        ArrayList<String[]> rs2 = Rd.ReadCSVFile(DataFile);

        NewModel.AddCSVData(rs2);
        System.out.println("Rows: " +NewModel.getRowCount());
        System.out.println("Cols: " +NewModel.getColumnCount());

    }
}


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(",");
                Rs.add(OneRow);
                System.out.println(Arrays.toString(OneRow));
                }
    }//end try 
        catch(Exception ex){
        String errmsg = ex.getMessage();
        System.out.println("File not Found: "+errmsg);
    }//end exception handling
    return Rs;
  }//End of ArrayList_readCSVFile class
}//End of CSVFile class

private static void createAndShowGui()
{
    //setup Window
    JFrame frame = new JFrame("PaymentImport");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //setup the conentPane
    Asist_payment_import_v1 newContentPane = new Asist_payment_import_v1();
    frame.setContentPane(newContentPane);

    //display the window
    frame.pack();
    frame.setVisible(true);

}


class MiModel extends AbstractTableModel
{
    private String [] columnNames = {"1","2","3","4","5","6","7","8","9","10",
                                    "11","12","13","14","15","16","17","18"};
    private ArrayList<String[]>Data = new ArrayList<>();

    public void AddCSVData(ArrayList<String[]> DataIn){
        this.Data = DataIn;
        this.fireTableDataChanged();
    }

    @Override
    public int getColumnCount(){
        return columnNames.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];
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // schedule a job for the event-despatching thread:
    //creating and showing this application's GUI
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGui();
        }
    });
    }}

Here is the result That I get: [Results of the Import program ][1]

Before the below results it shows all the contents of the CSVFile then it shows the amount of Columns and the amount of Rows that it has.

I want the result to show the contents in a JTable.

here is the exception I get From the output Window

[2019023, 294133, 69.00, 20171127, 11, 2017, "False", "Annamarie", 
"Group Payment (19061)", "False", "All", 0, , , "2017-11-27 09:43:14", "", 
54.00, 20171127]
Exception in thread "AWT-EventQueue-0" 
java.lang.ArrayIndexOutOfBoundsException: 16
[2019025, 288623, 130.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19063)", "False", "All", 0, , , "2017-11-27 09:43:30", "", 1068.80, 
20171127]
[2019027, 288625, 105.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19063)", "False", "All", 0, , , "2017-11-27 09:43:31", "", 855.00, 
20171127]
Rows: 690551
Cols: 18

at asist_payment_import_v1.Asist_payment_import_v1$MiModel.getValueAt(Asist_payment_import_v1.java:148)

at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5706)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 16 at asist_payment_import_v1.Asist_payment_import_v1$MiModel.getValueAt(Asist_payment_import_v1.java:148)

at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5706)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:227)
at java.awt.Dialog.show(Dialog.java:1084)
at javax.swing.JFileChooser.showDialog(JFileChooser.java:758)
at javax.swing.JFileChooser.showOpenDialog(JFileChooser.java:656)
at asist_payment_import_v1.Asist_payment_import_v1.actionPerformed(Asist_payment_import_v1.java:62)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  • @AndrewThompson what do you mean I have not implemented the requirement? I posted the question because I could not see what I did wrong, the Code I posted should already display the Data in the JTable but it does not display in as it should. I was hoping some 1 here can help me see where I WENT WRONG? – Fortune Dludla Jan 22 '18 at 10:20
  • I spoke too soon (& was incorrect). It seems the code **was** an attempt to implement the requirement. My bad. – Andrew Thompson Jan 22 '18 at 10:26
  • lol it is okay mate, I guess the way I phrased the question didn't help either, ...Thanks for the Edit – Fortune Dludla Jan 22 '18 at 10:35
  • I edited the post to include the question and improve it a little before I looked at the screen shot. 1) Text can be copied from the IDE's output window. Select the text to be copied, then right click the mouse for the 'copy option'. I mention that because textual information is a lot more useful than a screen shot. 2) Please copy / paste the (last part of the) CSV parsing and the exception into the question. You probably need to look more closely at the stack trace to discover the problem. If you cannot find / fix it from that, we'll need a [mcve] - so we can match up the .. – Andrew Thompson Jan 22 '18 at 10:37
  • .. line numbers from the stack trace to the source. See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Jan 22 '18 at 10:38

0 Answers0